[英]File not found when running jar from command line of a NetBeans project
I wrote a Java Application using NetBeans. 我使用NetBeans编写了Java应用程序。 It reads text file from the project's root directory.
它从项目的根目录读取文本文件。 When I run the jar file from command line it can't find the required file.
当我从命令行运行jar文件时,找不到所需的文件。
String inputFile = "input.txt";
Properties prop = new Properties();
String targetFormat = null;
try {
InputStream input = new FileInputStream("book-info-converter.properties");
prop.load(input);
targetFormat = prop.getProperty("targetFormat");
}
catch(IOException ex){
System.out.println("file not found");
}
How to solve this problem? 如何解决这个问题呢? Where to put my file so that the application can find that when running jar from command line?
在哪里放置我的文件,以便应用程序可以从命令行运行jar时找到它?
Place the book-info-converter.properties in the src
folder (it's better in resources
if you have). 将book-info-converter.properties放在
src
文件夹中(如果有的话,最好使用resources
)。 Then you can load it as resource stream like 然后您可以将其作为资源流加载
InputStream input = YourClass.class.getResourceAsStream("/book-info-converter.properties");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.