2014-03-31 58 views
0

嘿我要檢查,從那裏我的罐子運行(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目錄位置它仍然會拋出一個異常有人可以告訴我什麼是這個問題我是新來的這項工作感謝

+0

「jar目錄」,即jar中的目錄嗎? – fge

+0

一個包含我的jar的文件夾(檢查包含我的jar的文件夾是否有myfol) –

+0

啊,好的,這個不清楚。您應該直接使用'Path',然後使用'.resolveSibling()' – fge

回答

0

使用此:

final Path dir = Paths.get(PackLoader.class.getProtectionDomain() 
    .getCodeSource().getLocation().getPath()); 

return Files.isDirectory(dir.resolve("myfol")); 
+0

它仍然給我同樣的錯誤 –

+0

什麼錯誤?你在你的問題中說,拋出了一個異常,但你實際上沒有顯示它 – fge

相關問題