如何在构建 java 应用程序后使属性文件可用
how to make a properties file available after building a java application
我正在开发一个 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
我的问题是如何使我的属性文件在构建 May 应用程序后起作用?
我正在使用 netbeans,这是我的项目结构:
-源码包
--默认包
-- inputs.properties
--myapppackage
--myapppackage.java
提前致谢。
尝试使用它来加载属性文件:
File file = new File("inputs.properties);//use the right path
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.load(fileInput);
fileInput.close();
请在您的项目中创建一个文件夹 "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 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
我的问题是如何使我的属性文件在构建 May 应用程序后起作用?
我正在使用 netbeans,这是我的项目结构:
-源码包
--默认包
-- inputs.properties
--myapppackage
--myapppackage.java
提前致谢。
尝试使用它来加载属性文件:
File file = new File("inputs.properties);//use the right path
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.load(fileInput);
fileInput.close();
请在您的项目中创建一个文件夹 "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());
}