Question

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
Was it helpful?

Solution

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

OTHER TIPS

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

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:

enter image description here

Output:

enter image description here

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

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

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

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top