2

I have some Java program that uses 30 jars.

Today I know that I can do that like:

java -cp 1.jar;2.jar.....;30.jar; MyApplication -param1 AA -param2 BB

Does it seem messy?

How can I group all jars to one group (file), like Eclipse does with .classpath

Thank you,

Maxim Shoustin
  • 76,444
  • 28
  • 192
  • 219

5 Answers5

4

You can use wildcards to add all files from a particular folder, as demonstrated here

Community
  • 1
  • 1
2

Does that seem messy ? Absolutely! In these situations I often write a batch/shell script to simply invoke the java -cp command.

The advantage of having a specific script is that you don't rely on an environment variable (CLASSPATH) and so different scripts/executables can use different sets of jars.

Note that if you're building your project using Ant/Maven etc., they will provide options to run your program using the same classpath (or derivatives thereof) that you built it with.

e.g.

$ mvn exec:java
Brian Agnew
  • 254,044
  • 36
  • 316
  • 423
  • In my case I run from external program list of java applications where each is based on group of jars (hadoop/hive/jmx .....). – Maxim Shoustin Aug 06 '13 at 14:33
1

use an environment variable, and pass it to the -cp option

Starting with Java 6, you can use wildcard option to include multiple JARs in a directory. This is an example:

java.exe -classpath $HOME/myjars/* Main
Miguel Prz
  • 13,227
  • 26
  • 36
1

If your Java program is bundled as a jar file, you can use the class path attribute in the manifest file of the jar to declare the other jars that are needed to run your program. By doing this you only have to list your own jar on the command line.

See the answer of oxbow_lakes in Setting multiple jars in java classpath

Community
  • 1
  • 1
mschenk74
  • 3,501
  • 1
  • 19
  • 33
1

Unix solution:

To make it semi-dynamic and still easy to use in your terminal, you can add an alias to your .bashrc / .bash_profile which stores the included jars.

Adding alias javal="java -cp 1.jar:the-rest-of-your-jars.jar:2.jar:30.jar:" to your bash profile will make it possible to just execute javal MyApplication -param1 AA -param2 BB. When you want to add more jars, just update your .bashrc / .bash_profile. Of course you can use any commandname instead of javal.

Easy peasy explanation about aliasses here.

-edit- thanks @mschenk74

rsplak
  • 11,428
  • 2
  • 31
  • 30