Reputation: 12796
I have a jar with main-class which can be executed like: java -jar test.jar
Inside the jar I have something like
public static void main(String[] args) throws IOException {
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("config.properties");
Properties prop = new Properties();
prop.load(is);
//then I wanna fetch all the properties in the config.properties file
}
I run both:
java -jar test.jar
java -jar test.jar -cp /tmp (where config.properties is located)
java -jar test.jar -cp /tmp/config.properties (obviously it doesn't work, but give you the idea what I am trying to achieve here)
The code didn't work, all three throw NPE although I put the path of the config.properties file under my $PATH and $CLASSPATH.
The point is that, in long run, I will put the configuration file in ${my-config-path}, and read/handle it properly. But temporally I just want something quick and dirty.
Upvotes: 3
Views: 12442
Reputation: 51
One sample example
@Echo off
java -cp "C:/***/***" -Dlog4j.configuration=file:C:/***/***/log4j.properties com.****.****.MainMethod %1 %2 ...
pause
Upvotes: 0
Reputation: 160291
Once you specify -jar
all classpath options are ignored.
Instead, specify a default config location (like in user's home directory) and allow overriding on the command line.
There are a variety of command line parsing options, the easiest annotate class properties with option information, e.g., the long and short option names, usage, etc.
Or use a -D
option and retrieve the appropriate system property.
Another option is the Preferences
API.
Upvotes: 5