Reading files in Java Web Start
I have an application that has some 3rd party libraries that require some configuration files to exist. I put these in jar files so they can be deployed using JWS. I know that you can read files from the jar file using getResourceAsStream(), but the 3rd party libraries don't do that. I have tried reading them out of the jar with getResourceAsStream and writing the actual files to user.dir. The application works fine that way. However, it seems that most of the users launch the application from a shortcut on the deskop, so of course their user.dir is the desktop. They would rather not litter their desktop with configuration files.
Is there some better way I can handle t开发者_JAVA技巧his?
You can actually change your user.dir. I think it is "untouchable" at first.
So first you need to make it "touchable"
We invoke the following using a static block for our class MainWindow
static {
try {
Class clazz = ClassLoader.class;
Field field = clazz.getDeclaredField("sys_paths");
boolean accessible = field.isAccessible();
if (!accessible) {
field.setAccessible(true);
}
field.set(clazz, null);
try {
String currentDir = System.getProperty("java.library.path");
//now remove the current directory from the library path.
String sansCurrentDir = removeCurrentDirectoryFromPath(currentDir);
System.setProperty("java.library.path", sansCurrentDir );
}
finally {
field.setAccessible(accessible);
}
} catch (Exception e) {
//in our case we rethrow
}
}
//and then later in our code in the same static block we can now create our root directory (ie, the user.dir) and execute
System.setProperty("user.dir", rootDirectory);
精彩评论