General discussion
-
Topic
-
Can I use -cp argument twice when executing Java
I am trying to run a Java application which has many dependencies. In the past I have use the following command to launch the application
java -cp “program.jar:jar1.jar:jar2.jar:jar3.jar:[…]” program
However as the list of dependencies have grown, I have moved them into an arguments file, the contents of this file are:-cp “\
program.jar:\
jar1.jar:\
jar2.jar:\
jar3.jar:\
[…]”
And I am running the application withjava @arguments-file program
Everything up to this point works fine.Sometimes I end up with beta versions of program.jar, they share all of the same dependencies, but program.jar is renamed program-beta.jar.
So to run the jar the following command would be used
java -cp “program-beta.jar:jar1.jar:jar2.jar:jar3.jar:[…]” program
or more specifically, I would use an environment variable, so that the same script can be used, and the variable would be set to either program.jar, or program-beta.jar, depending on the circumstancejava -cp “$PROGRAM_JAR:jar1.jar:jar2.jar:jar3.jar:[…]” program
Now that I am using an arguments file I was hoping to be able to be able to do something like:java -cp “$PROGRAM_JAR” @arguments-file program
However using -cp twice causes one of the two to be ignored, resulting in a java.lang.ClassNotFoundException exception.Is there any way around this that allows me to specify one jar file by name, but abstract away all of the others so that my java command isn’t thousands of characters?
This will be running entirely on Linux, so any command line “magic”, such as using grep is fine, so long as the resulting code is easily readable