java - 如何在构建 java 应用程序后使属性文件可用

标签 java swing properties-file

我正在开发一个 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/

相关文章:

java - 由 : java. lang.ClassNotFoundException : org. springframework.context.event.EventListenerFactory - Spring Batch 引起

java - 动态设置 Java Web 应用程序的资源

java - 如何避免这么多 if else 语句

java - JMenuItem 更新无需重新启动客户端

spring - 如何使用 @ConfigurationProperties 注入(inject) java.nio.file.Path 依赖项

java - 应用函数和过程的使用

java - 媒体样式 : RemoteServiceException: Bad notification posted from package

java - 是否有可以在 JTables 中使用的通用 TableModel?

java - 在 Spring Boot 中的 application.properties 文件中定义映射列表

java - messages.properties 键具有多个点不起作用