0

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 ?

5
  • 1
    Resources are loaded by classloaders, so you would need to run your jar not with -jar but with -classpath and include the directory in which your config files reside as part of the classpath, along with the jar itself. Commented Nov 10, 2024 at 17:40
  • This question is similar to: What is a classpath and how do I set it?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Nov 10, 2024 at 18:21
  • Please provide enough code so others can better understand or reproduce the problem. Commented Nov 10, 2024 at 22:55
  • Thanks @GaëlJ and g00se for your replies , this is very informative. Just edited the question with more details , will you please check for updated , thanks. Commented Nov 11, 2024 at 7:34
  • So you want java -classpath ExecutableJAR.jar;C:\User\lib a.b.c.Main. Odd directory btw. You shouldn't normally create directories off the root Commented Nov 11, 2024 at 11:20

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.