General discussion

  • Creator
    Topic
  • #3990237

    Can I use -cp argument twice when executing Java

    by soupsfandom.0c ·

    Tags: 

    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

You are posting a reply to: Can I use -cp argument twice when executing Java

The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our Community FAQs for details. All submitted content is subject to our Terms of Use.

All Comments

Viewing 0 reply threads