is it possible to add to the classpath of a runnable jar file some properties file? I tryed these solutions solutions:
running the executable file using the
You can simply put the properties file in the META-INF folder.
+META-INF
+properties_dir
+Test.properties
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:
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
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");
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");
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