【问题标题】:Java eclipse error with exported runnable jar file导出可运行 jar 文件的 Java eclipse 错误
【发布时间】:2015-07-30 22:45:14
【问题描述】:

导出的 jar 文件有问题。当我在 Eclipse 中运行我的项目时,它运行良好,但是当我从控制台将其作为导出的 jar 运行时,我收到以下错误消息:

java.io.FileNotFoundException: firstLaunch.properties (System can't find file)
or
java.io.FileNotFoundException: resources/config/firstLaunch.properties (System can't find file)

我尝试将其放入资源文件夹并将语法从firstLaunch.properties 更改为/resource/config/firstLaunch.properties,但它再次说了同样的话,但路径不同。我不知道为什么要这样做。

代码如下:

public void saveConfigFile(String file, String key, String value) {
    Properties prop = new Properties();
    OutputStream output = null;

    try {

        output = new FileOutputStream(file);

        // set the properties value
        prop.setProperty(key, value);

        // save properties to project root folder
        prop.store(output, null);

    } catch (IOException io) {
        io.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

我执行该方法的语法是

if (properties.loadConfigFile("firstLaunch.properties", "value").equals(properties.loadConfigFile("true.properties", "true"))) {
        properties.saveConfigFile("port.properties", "port", "8795");
        properties.saveConfigFile("ip.properties", "ip", temp[1]);
        properties.saveConfigFile("firstLaunch.properties", "value", "false");
        settings.port = properties.loadConfigFile("port.properties", "port");
        settings.myIp = properties.loadConfigFile("ip.properties", "ip");
    } else {
        settings.port = properties.loadConfigFile("port.properties", "port");
        settings.myIp = properties.loadConfigFile("ip.properties", "ip");
    }

【问题讨论】:

    标签: java eclipse


    【解决方案1】:

    您的问题可能与您引用文件位置的方式有关。添加一些关于您如何引用代码的详细信息/代码示例,这样我们就可以确保提供帮助。话虽如此,这是引用属性文件的另一种方式:

    像这样放在类路径中:

    private static Properties prop = new Properties();
    private static String filename = "<name of file>.properties";
    InputStream input = <ClassName>.class.getClassLoader().getResourceAsStream(filename);
    try {
    
        if (input==null) {
            loggerOut.log(Level.SEVERE, "Sorry, unable to find " + filename);
        }
        prop.load(input);   
        loggerOut.info("XML In storage path: " prop.getProperty("<property in file>"));
        fileNameAndPath = prop.getProperty("fileNameAndPathIN").trim();
        logNameAndPath = logPath + logName;
    
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input!=null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-13
      • 2012-12-06
      • 2023-03-19
      • 2013-02-21
      • 2017-05-13
      • 2014-05-09
      • 1970-01-01
      • 2012-11-23
      • 2014-05-06
      相关资源
      最近更新 更多