导出的runnable jar文件出现Java eclipse错误

时间:2015-07-30 13:55:00

标签: java eclipse

导出的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");
    }

1 个答案:

答案 0 :(得分:0)

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

将它放在类路径中:

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();
        }
    }
}