hi
does anybody know a way to read out the absolute path of a class or a jar in the filesystem? something like project-root. I’m not talking about current directory or so, but the directory where one can find the jar file.
Qudus
hi
does anybody know a way to read out the absolute path of a class or a jar in the filesystem? something like project-root. I’m not talking about current directory or so, but the directory where one can find the jar file.
Qudus
If there would you’ll get a whole bunch of paths, because you can have more than one jar containing your program. + What about all the jars that make up the jre?
If you are looking for currrent working directory its in the property user.dir
There are lots of other useful tidbits liek that in properties. Look at the JavaDocs for more info
If you mean the jar file from which classes are loaded, then unless you are implementing a Java IDE you don’t want to do this. If what you want is a list of the resources/classes available, then add a file containing that to the jar and load it as a resource.
I’ve done a thing like this (in the beginning of the main class) to access the config and other files in the program’s installation directory. It looks from the “java.class.path” property the location of the JAR file and changes the program’s home directory there. This made it possible to start the program in any directory by double-clicking a file associated with the program.
public static final File STARTUP_DIRECTORY = new File(System.getProperty("user.dir")).getAbsoluteFile();
public static final String PROGRAM_JAR_NAME = "ikayaki.jar";
static {
// change to the program directory if it was started somewhere else
String[] paths = System.getProperty("java.class.path").split(System.getProperty("path.separator"));
for (String s : paths) {
File file = new File(s);
if (file.getName().equals(PROGRAM_JAR_NAME)) {
file = file.getAbsoluteFile();
System.setProperty("user.dir", file.getParent());
break;
}
}
}
public static final File PROPERTIES_FILE = new File("ikayaki.config").getAbsoluteFile();
...
Simply do this like Jeff was saying:
System.getProperty(“user.dir”);
This is bad idea, it has the same unexpected effects as noted in this bug
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4117557
Also note that classes/jars can be loaded from locations which do not appear on the classpath.