导出的runnable jar文件出现Java eclipse错误(Java eclipse error with exported runnable jar file)
我有一个导出的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");
}
I have a problem with an exported jar file. When I run my project in Eclipse it runs fine, but when I run it as an exported jar from the console I receive the following error message:
java.io.FileNotFoundException: firstLaunch.properties (System can't find file)
or
java.io.FileNotFoundException: resources/config/firstLaunch.properties (System can't find file)
I tried to put it in to the resource folder and change syntax from firstLaunch.properties to /resource/config/firstLaunch.properties, but again it says the same thing but with a different path. I don't know why is this doing this.
Here is the code:
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();
}
}
}
}
My syntax for executing the method is
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");
}
原文:https://stackoverflow.com/questions/31725729
更新时间:2020-01-27 21:05
最满意答案
您的问题可能与您引用文件位置的方式有关。 添加一些关于如何引用代码的详细信息/代码示例,以便我们确保提供帮助。 这里说的是另一种引用属性文件的方法:
把它放在类路径中,如下所示:
private static Properties prop = new Properties();
private static String filename = ".properties";
InputStream input = .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(""));
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();
}
}
}
Your problem probably has to do with how you are referencing the file location. Add some detail/code examples as to how you are referencing the code, so then we can be sure to help. Having said that here is another way to reference a properties file:
Put it in the classpath like this:
private static Properties prop = new Properties();
private static String filename = ".properties";
InputStream input = .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(""));
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();
}
}
}
2015-07-30
相关问答
添加一个包含如下内容的脚本: #!/bin/bash
java -Djava.library.path=project_lib/native/ -jar project_lib/jri.jar
我以这种方式导出一些Java项目。 Add a script containing something like that: #!/bin/bash
java -Djava.library.path=project_lib/native/ -jar project_lib/jri.jar
I expor
...
可运行的jar包含一个MANIFEST.MF文件,它定义了运行jar时要执行的Main类。 不可运行的jar只是类的库,可以添加到类路径,以便代码被重用(它还包含清单文件,但没有主类) The runnable jar contains a MANIFEST.MF file, which defines the Main class to be executed when the jar is run. Non-runnable jars are just libraries of classes
...
这基于内存,但我很确定属性文件不会从jar自动加载。 我们的想法是属性文件应该很容易修改。 否则,值也可能只是在类文件中。 如果您尝试从其他任何位置访问属性文件,这应该不是问题。 这也是您很少看到单独打包jar文件的原因。 通常还需要加载其他东西。 这种行为是否有必要? This is based on memory, but I'm fairly sure that properties files aren't automatically loaded from a jar. The idea
...
这个是正确的: public class MainApp {
public static void main(String[] args) {
JFrame frame = new JFrame("Testing");
ImageIcon icon = createImageIcon("/resources/logo.png");
JLabel label1 = new JLabel("Image and Text", icon, JLab
...
您的目录内容应如下所示: 如果您随后使用java -jar application.jar启动应用java -jar application.jar则wordfile将相对于您的代码定位,并在您使用./wordfile.docx寻址时打开。 Your directory contents should look like this: If you then start your application with java -jar application.jar the wordfile is p
...
尝试点击“将所需的库打包到生成的JAR中”选项。 JAR文件是一个常规的压缩存档。 确保MANIFEST.MF文件中的所有必需库存在于生成的文件中。 Try to click on the option “Package required libraries into generated JAR”. A JAR file is a regular compressed archive. Make sure that all the required libraries in your MANIFE
...
这是以前类似问题的链接 。 他们有一些很好的解决方案。 一个简单的尝试开始是JarFix ,它将用正确的java exe重新关联.jar文件。 如果您的程序在运行时立即关闭,请尝试在其末尾添加片段以使进程保持活动状态。 System.out.println("Press enter to close.");
try {
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
Here's a link to
...
您的问题可能与您引用文件位置的方式有关。 添加一些关于如何引用代码的详细信息/代码示例,以便我们确保提供帮助。 这里说的是另一种引用属性文件的方法: 把它放在类路径中,如下所示: private static Properties prop = new Properties();
private static String filename = ".properties";
InputStream input = .class.getClassLo
...
好的,所以你可以将问题标记为已关闭: 如果您的Java安装注册了.jar扩展名,它将尝试运行Jar(我猜这就是发生的事情)。 但它会悄悄地这样做。 为了查看发生了什么(错误等),您应该尝试使用java -jar myfile.jar从命令行运行它。 Ok then, so you can mark the question closed: If your Java installation is registered for the .jar extension it will try to ru
...
这听起来像是你为你的jar选择了错误的主要方法。 您的应用程序是否有多个启动配置? 确保在导出时选择正确的启动配置。 Ben Lawry was correct, my properties files were not being accessed properly, and since they were loaded before the GUI appeared, program failed upon startup without showing anything...
















