检查jar目录是否包含文件夹

时间:2014-03-31 13:56:31

标签: java file jar path

嘿,我想检查我的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目录对我来说没什么问题但是当我的jar目录位置中存在一个目录myfol时会出现一些问题它仍会抛出一个异常可以有人告诉我这是什么问题我是这个工作的新手谢谢

1 个答案:

答案 0 :(得分:0)

使用此:

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

return Files.isDirectory(dir.resolve("myfol"));
相关问题