当我从 jar 运行应用程序时,为什么 new File(path)
不起作用?我应该如何改变这个?现在,当我的应用程序尝试加载文件时,我收到 FileNotFound
异常。
final File file = new File("src/main/resources/maps/ParcelsCountyRDMFinal5.shp");
最佳答案
如果正确创建了 jar,例如使用 Maven,资源文件将位于类文件旁边。它看起来像这样:
myapp.jar
|
+--- com
| +---mycompany
| +-- FileOpsClass
|
+--- maps
+-- fileToRead
您可以进入目录并访问fileToRead
,如上述结构所示:
final File file = new File("../../maps/ParcelsCountyRDMFinal5.shp");
或按照建议
final File file = new File(getClass().getResource("/maps/ParcelsCountyRDMFinal5.shp"));
关于java - 当我从 jar 运行应用程序时,为什么 new File(path) 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9293140/