你實際上可以改變你的user.dir。起初我認爲它是「不可觸摸的」。
所以首先你需要使它「可觸摸」
我們調用使用靜態塊我們班以下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);