简体   繁体   English

从NetBeans项目的命令行运行jar时找不到文件

[英]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.

相关问题 从命令行运行 .jar 时,NetBeans 8.2 中指定的命令行参数会出现 - Command line parameters specified in NetBeans 8.2 turns up when running .jar from command line 从命令行使用jar文件运行junit - running junit from command line with jar file Java应用程序可以从netbeans正常运行,但不能从命令行或.jar文件运行 - Java application works fine from netbeans but not the command line or a .jar file 从命令行将jar2exe文件作为jar运行 - Running a jar2exe file as jar from command line Springboot:从命令行运行时找不到jar异常 - Springboot: Jar not found exception while running from command line 如何在没有jar文件的命令行中编译和运行Netbeans java项目? - How to compile and run a Netbeans java project in command line without jar file? 在 jar 文件上通过命令行运行时出现 SLF4J.NoClassDefFoundError - SLF4J .NoClassDefFoundError when running by command line on jar file 从命令行运行gradle时如何将自定义JAR文件添加到CLASSPATH? - How to add a custom JAR file to CLASSPATH when running gradle from command line? 运行Jar时找不到文件 - File not found when running Jar 从Java代码(NetBeans)运行.JAR文件 - Running .JAR file from java code (netbeans)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM