Java 添加属性文件以构建可运行 jar 的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19424308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add properties file to build path of runnable jar
提问by Daniele Milani
is it possible to add to the classpath of a runnable jar file some properties file? I tryed these solutions solutions:
是否可以将某些属性文件添加到可运行 jar 文件的类路径中?我尝试了这些解决方案解决方案:
running the executable file using the following command:
java -cp ../prop_dir/prop1.properties;../prop_dir/prop2.properties -jar MyRunnableJar.jar
adding to the MANIFEST FILE (in the Class-Path section)
../prop_dir/prop1.properties ../prop_dir/prop1.properties
使用以下命令运行可执行文件:
java -cp ../prop_dir/prop1.properties;../prop_dir/prop2.properties -jar MyRunnableJar.jar
添加到清单文件(在类路径部分)
../prop_dir/prop1.properties ../prop_dir/prop1.properties
but none of them works.
但它们都不起作用。
The architecture of the running dir is the following
运行目录的架构如下
+
+ MyRunnableJar.jar
+ prop_dir/
+ prop1.properties
+ prop2.properties
Thanks so much,
非常感谢,
Daniele
丹尼尔
EDIT
编辑
When I execute the following line
当我执行以下行时
System.out.println(System.getProperty("java.class.path"));
I obtain in my console
我在我的控制台中获得
MyRunnableJar.jar
采纳答案by Daniele Milani
In order to solve this problem i used a workaround: instead of running the runnable jar i runned the main class and I added the jar to the classpath.
为了解决这个问题,我使用了一种解决方法:我没有运行可运行的 jar,而是运行了主类,并将 jar 添加到类路径中。
The command I used is the following one:
我使用的命令如下:
java -classpath .;MyRunnableJar.jar;prop_dir; my.package.MyClass
回答by Keerthivasan
You have to reference the .properties file in your class file relative to the .jar file location. The classpath seperators are different for Windows (semicolon) and Unix environments (colon).
您必须在相对于 .jar 文件位置的类文件中引用 .properties 文件。Windows(分号)和 Unix 环境(冒号)的类路径分隔符是不同的。
For windows
窗户用
java -classpath .;prop_dir; -jar Runnable.jar (In your Class file, the properties file should be accessed by relative URl from classpath say "prop_dir/prop1.properties")
java -classpath .;prop_dir; -jar Runnable.jar(在您的类文件中,属性文件应该由类路径中的相对 URl 访问,例如“prop_dir/prop1.properties”)
For Unix env
对于 Unix 环境
java -classpath .:prop_dir: -jar Runnable.jar (In your Class file, the properties file should be accessed by relative URl from classpath say "prop_dir/prop1.properties")
java -classpath .:prop_dir: -jar Runnable.jar (在你的类文件中,属性文件应该由类路径中的相对 URl 访问,比如“prop_dir/prop1.properties”)
Code:
代码:
Output:
输出:
回答by Admit
should it be like this prop_dir/prop1.properties prop_dir/prop1.properties
in manifest?
prop_dir/prop1.properties prop_dir/prop1.properties
清单上应该是这样吗?
Also checkout this question: Java -jar : access external configuration file
回答by prmsheriff
Adding a . to Class-Path of MANIFEST.MF and placing the properties file in the same level as the runnable jar works for me.
添加一个 . 到 MANIFEST.MF 的类路径,并将属性文件放置在与可运行 jar 相同的级别对我有用。
java -jar MyExec.jar
MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.test.MyMain
Class-Path: . lib/MyDependent1.jar lib/MyDependent2.jar
Code snippet to load properties file should be like
加载属性文件的代码片段应该像
ResourceBundle properties = ResourceBundle.getBundle("MyExec");
回答by shalakha
You can simply put the properties file in the META-INF folder.
您可以简单地将属性文件放在 META-INF 文件夹中。
+META-INF
+properties_dir
+Test.properties
回答by ashah
What worked for me is adding properties file in jar and put that jar in classpath in MANIFEST file (under Class-Path section).
对我有用的是在 jar 中添加属性文件,并将该 jar 放在 MANIFEST 文件的类路径中(在 Class-Path 部分下)。
Somehow it didn't work when I pass that same jar under -cp option. Seems it always take classpath from MANIFEST file for runnable jar.
当我在 -cp 选项下传递同一个 jar 时,它以某种方式不起作用。似乎它总是从 MANIFEST 文件中获取可运行 jar 的类路径。
回答by SujJi
I have a better approach to solve this
我有更好的方法来解决这个问题
My project structure is :- You can see there is no property file in this project structure
我的项目结构是:-你可以看到这个项目结构中没有属性文件
package com.main;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Run {
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream stream = loader.getResourceAsStream("config.properties");
prop.load(stream);
System.out.println(prop.getProperty("name"));
}
}
Now place the property file within the same folderwhere you have exported the runnable jar file.
现在将属性文件放在导出可运行 jar 文件的同一文件夹中。
For this approach you don't need to create a property file in eclipse you just have to create a property file where you export the runnable jar and remember don't give the path of a property file, write only the property file name like this
对于这种方法,您不需要在 eclipse 中创建属性文件,您只需创建一个属性文件,在其中导出可运行的 jar 并记住不要提供属性文件的路径,只写这样的属性文件名
InputStream stream = loader.getResourceAsStream("config.properties");
InputStream stream = loader.getResourceAsStream("config.properties");