简体   繁体   English

添加属性文件以构建可运行jar的路径

[英]Add properties file to build path of runnable jar

is it possible to add to the classpath of a runnable jar file some properties file? 是否可以将一些属性文件添加到可运行的jar文件的类路径中? I tryed these solutions solutions: 我尝试了以下解决方案:

  1. running the executable file using the following command: 使用以下命令运行可执行文件:

    java -cp ../prop_dir/prop1.properties;../prop_dir/prop2.properties -jar MyRunnableJar.jar java -cp ../prop_dir/prop1.properties;../prop_dir/prop2.properties -jar MyRunnableJar.jar

  2. adding to the MANIFEST FILE (in the Class-Path section) 添加到清单文件中(在“类路径”部分中)

    ../prop_dir/prop1.properties ../prop_dir/prop1.properties ../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

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

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 还要签出这个问题: Java -jar:访问外部配置文件

You have to reference the .properties file in your class file relative to the .jar file location. 您必须在类文件中相对于.jar文件位置引用.properties文件。 The classpath seperators are different for Windows (semicolon) and Unix environments (colon). Windows(分号)和Unix环境(冒号)的类路径分隔符不同。

For windows 对于窗户

java -classpath .;prop_dir; 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") -jar Runnable.jar(在您的Class文件中,相对URl应该从类路径中说“ prop_dir / prop1.properties”来访问属性文件)

For Unix env 对于Unix env

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(在您的Class文件中,应该由相对URl从类路径中访问属性文件,说“ prop_dir / prop1.properties”)

Code: 码:

在此处输入图片说明

Output: 输出:

在此处输入图片说明

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的Class-Path并将属性文件与可运行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");

You can simply put the properties file in the META-INF folder. 您可以简单地将属性文件放在META-INF文件夹中。

+META-INF
 +properties_dir
  +Test.properties

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. 当我在-cp选项下传递相同的jar时,它不起作用。 Seems it always take classpath from MANIFEST file for runnable jar. 似乎它总是从MANIFEST文件中获取可运行jar的类路径。

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 folder where 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM