Reputation: 11
Have a jar file of an application, there are 2 different external configuration files which needs to be execute during execution of jar file as the backend code of application needs some property reading from these config files.
Question is how can I use these config files, so that while executing the jar , these config files can be considered. Should there be some path specified at code level for config files or some more argument to be added in "java -jar" command. Though this seems to be a basic java development question , yet very new to software development and any sort of suggestion will be helpful.
executing jar by command line " java -jar ExecutableJAR.jar"
Edit : The data inside These configuration files are though not fixed and the key value pairs inside them are suppose to be change based on environment the jar executes. Will that makes any change in the approach , or using the classPath method still works ?
MyClass.Main class is on "org.framework.emulator" package and the execution of main class starts some other service class which is in "org.framework.emulator.service" package. This service class needs those 2 external config file properties to read from.
Currently have these config files (file1 , file2) in ".txt" format in "C:\User\lib" folder. So , do I need to provide the relative path of "file1 and file2" in service class code , and also include the "C:\User\lib" path in classpath option as well ?
Upvotes: 0
Views: 59
Reputation: 12817
If you have control over the main
method, you can just pass the files on the command line and pick them up in the main
method:
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("must have 2 file arguments");
System.exit(-1);
}
File f1 = new File(args[0]);
File f2 = new File(args[1]);
// use files
// start application
}
java -jar executable.jar F1 F2
Another approach is to use system properties to communicate the paths to the files.
java -Df1=F1 -Df2=F2 -jar executable.jar
and pick them up using System.getProperty()
.
Upvotes: 1