【发布时间】:2014-05-10 21:44:48
【问题描述】:
嘿,我想检查一下我的 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 时出现一些问题,它仍然会引发异常,有人可以告诉我有什么问题我是这项工作的新手 谢谢
【问题讨论】:
-
一个“jar目录”,即jar中的目录?
-
包含我的 jar 的文件夹(检查包含我的 jar 的文件夹是否有 myfol)
-
啊,好吧,这个不清楚。您应该直接使用
Path,然后使用.resolveSibling() -
没有得到你的评论@fge
-
resolveSibling() 方法在哪里,哈哈