SiteBackend/src/main/java/com/alttd/forms/properties/JarPath.java

28 lines
861 B
Java

package com.alttd.forms.properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Optional;
public class JarPath {
private static final Logger logger = LoggerFactory.getLogger(JarPath.class);
public static Optional<File> getCurrentJarDir() {
try {
String path = JarPath.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
File parentFile = new File(path).getParentFile();
if (!parentFile.isDirectory()) {
logger.error("Parent file of jar is not a directory");
return Optional.empty();
}
return Optional.of(parentFile);
} catch (Exception e) {
logger.error("Error getting current jar directory", e);
return Optional.empty();
}
}
}