Add properties file to build path of runnable jar

≡放荡痞女 提交于 2019-12-09 01:38:08

问题


is it possible to add to the classpath of a runnable jar file some properties file? 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

  2. adding to the MANIFEST FILE (in the Class-Path section)

    ../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

回答1:


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.

The command I used is the following one:

java -classpath .;MyRunnableJar.jar;prop_dir; my.package.MyClass



回答2:


should it be like this prop_dir/prop1.properties prop_dir/prop1.properties in manifest?

Also checkout this question: Java -jar : access external configuration file




回答3:


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).

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")

For 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")

Code:

Output:




回答4:


Adding a . to Class-Path of MANIFEST.MF and placing the properties file in the same level as the runnable jar works for me.

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



回答5:


You can simply put the properties file in the META-INF folder.

+META-INF
 +properties_dir
  +Test.properties



回答6:


What worked for me is adding properties file in jar and put that jar in classpath in MANIFEST file (under Class-Path section).

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.




回答7:


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.

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

InputStream stream = loader.getResourceAsStream("config.properties");



来源:https://stackoverflow.com/questions/19424308/add-properties-file-to-build-path-of-runnable-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!