我正在开发一个 java swing 应用程序,它工作正常,但是在构建我的应用程序之后,当我运行 jar 文件时,我的应用程序无法按我想要的方式工作。所以要知道我使用此测试的问题是什么:
FileReader reader;
Properties props;
try{
reader = new FileReader("src\\inputs.properties");
props = new Properties();
props.load(reader2);
JOptionPane.showMessageDialog(null,reader.getClass());
}catch(Exception e){
JOptionPane.showMessageDialog(null,e.getMessage());
}
所以当我运行应用程序时,它工作正常,我收到以下消息: message before building the app
这意味着我的属性文件已加载。
但是当我构建应用程序时,当我运行它时,我收到此消息:
message after building the app
我的问题是如何使我的属性文件在构建可能的应用程序后工作?
我正在使用 netbeans,这是我的项目结构:
-源码包
--默认包
-- 输入.属性
--myapppackage
--myapppackage.java
提前致谢。
最佳答案
请在您的项目中创建一个文件夹“config”并将inputs.properties放入其中。
reader = new FileReader("config/inputs.properties");
有关详细信息,您还可以通过此线程: Netbeans FileReader FileNotFound Exception when the file is in folder?
或者: 当您的资源位于 JAR 文件中时,它就不再是文件了。文件只是文件系统上的物理文件。解决方案:使用getResourceAsStream。像这样的事情:
try {
Properties props;
Property p=new Property();
InputStreamReader in=new InputStreamReader(p.getClass().getResourceAsStream("/config/" + "inputs.properties"));
props = new Properties();
props.load(in);
JOptionPane.showMessageDialog(null, in.getClass());
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
关于java - 如何在构建 java 应用程序后使属性文件可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34556909/