Discussion Locked

Can I use -cp argument twice when executing Java

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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 with

java @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 circumstance

java -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

2 replies

by Ada James

As I read this post, I found it to be very helpful. Thank you for posting it. I enjoyed reading it.

This discussion is closed to new replies.