嘿我要檢查,從那裏我的罐子運行(JAR的路徑)目錄中包含特定的文件夾我試圖像:檢查JAR目錄中包含的文件夾
try{
File fofjar=new File((PackLoader.class.getProtectionDomain().getCodeSource().getLocation().getPath()));
File f=new File(fofjar.toString()+"\\myfol");
System.out.println(f.toPath());
if (Files.exists(f.toPath())) {
System.out.printlin("folder is there!")
}
else{
throw new Exception("\n"+f.toPath());
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, "directory does not exist\n" + e + "\nClass:Pack Loader", "Not Found Error", JOptionPane.ERROR_MESSAGE);
}
的問題是該jar目錄返回對我來說很好,但有一個問題,當目錄myfol存在於我的jar目錄位置它仍然會拋出一個異常有人可以告訴我什麼是這個問題我是新來的這項工作感謝
「jar目錄」,即jar中的目錄嗎? – fge
一個包含我的jar的文件夾(檢查包含我的jar的文件夾是否有myfol) –
啊,好的,這個不清楚。您應該直接使用'Path',然後使用'.resolveSibling()' – fge