简体   繁体   English

程序可以在编译器中正常工作,但不能在jar(Java)中

[英]Program works fine in compiler but not in jar (Java)

I Exported my program into a jar file but when I ran it, it seems as if the .properties file isn't found by the program. 我将程序导出到jar文件中,但是当我运行它时,似乎该程序找不到.properties文件。 I made sure it was in the jar file & it worked fine prior to exporting it. 我确保它在jar文件中,并且在导出之前工作正常。 I read something about using getClass().getResourceAsStream() instead of FileInputStream and FileOutputStream but can't seem to understand how that would help. 我读了一些有关使用getClass().getResourceAsStream()而不是FileInputStreamFileOutputStream但似乎无法理解这会如何帮助。 Any ideas? 有任何想法吗? These are the two methods that use the file. 这是使用文件的两种方法。

private void UpdateData() throws IOException{
        FileInputStream in = new FileInputStream("config.properties");
        Properties props = new Properties();
        props.load(in);
        in.close();

        FileOutputStream out = new FileOutputStream("config.properties");
        props.setProperty("prop1", prop1TextArea.getText().toString());
        props.setProperty("prop2", prop2TextArea.getText().toString());
        props.setProperty("prop3", prop3TextArea.getText().toString());
        props.store(out, null);
        out.close();

}

    private void setText() throws IOException {

        FileInputStream in = new FileInputStream("config.properties");
        Properties props = new Properties();
        props.load(in);
        in.close();

        FileOutputStream out = new FileOutputStream("config.properties");
        prop1TextArea.setText(props.getProperty("prop1"));
        prop2TextArea.setText(props.getProperty("prop2"));
        prop3TextArea.setText(props.getProperty("prop3"));
        out.close();
    }

To access resources in your .jar file you have to access them as resources not as files. 要访问.jar文件中的资源,您必须将它们作为资源而不是文件来访问。

The .properties are not in the filesystem anymore and therefore you cannot access them via FileInputStream . .properties不再在文件系统中,因此您无法通过FileInputStream访问它们。

If you want to save them afterwards you have to create new .properties on the first start of your program. 如果要在以后保存它们,则必须在程序的第一次启动时创建新的.properties So you have to check first if the file exists (eg via File.exists() ) and either work without the properties from the file or create a new file and use this then. 因此,您必须首先检查文件是否存在(例如,通过File.exists() ),然后在没有文件属性的情况下工作,或者创建一个新文件,然后使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java 程序(带有 txt/csv)在 IDE 中运行良好,但在 jar 中运行良好 - java program (with txt/csv) works fine in IDE but not in jar Java程序可以在Eclipse中正常运行,但不能作为.jar文件运行 - Java program runs fine in Eclipse but not as a .jar file Eclipse Java-程序可以在Eclipse中运行,但不能作为可运行的jar - Eclipse Java - Program works in eclipse but not as runnable jar Java程序-在Eclipse中工作,但在JAR中不工作-FreeTTS - Java Program - Works in Eclipse but Not in JAR - FreeTTS Java 新计算机上 DDE 中的编译器错误,在旧计算机上工作正常 - Java compiler errors in DDE on new computer, works fine in old one 通过反射运行main()时使用ClassNotFound,但是与java -jar一起使用时效果很好 - ClassNotFound when running main() through reflection but works fine with java -jar Java应用程序可以从netbeans正常运行,但不能从命令行或.jar文件运行 - Java application works fine from netbeans but not the command line or a .jar file Java程序可以在Eclipse中正常运行,但不能作为.jar文件运行(配置路径错误吗?) - Java program runs fine in Eclipse but not as a .jar file (Is the config path wrong??) 编译器错误,但程序执行正常 - Compiler error but program executes fine 在Eclipse中播放视频效果很好,但是当将Java项目导出为可运行的JAR文件时,它不再起作用 - Playing videos works fine in Eclipse but when exporting the java project as an runnable JAR file it doesnt work anymore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM