You use either -jar or -cp , you cannot combine these two. If you want to put additional JARs in the classpath, then you must either put them in the main JAR manifest and then use java -jar or you put the full class path (including the main JAR and its dependencies) in -cp and name the main class explicitly on the command line
java -cp 'MyProgram.jar:libs/*' main.Main
(I use the dir/* syntax, which tells the java command to add all .jar files from a specific directory to the class path. Note that * must be protected from expansion by the shell, so I used single quotes.)
You mentioned that you are using Ant, so for an alternative manifest, you can use the Ant <manifestclasspath> task after copying the dependencies, but before creating the JAR.
<manifestclasspath property="myprogram.manifest.classpath" jarfile="MyProgram.jar"> <classpath> <fileset dir="libs" includes="*.jar" /> </classpath> </manifestclasspath> <jar destfile="MyProgram.jar" basedir="classes"> <manifest> <attribute name="Main-Class" value="main.Main" /> <attribute name="Class-Path" value="${myprogram.manifest.classpath}" /> </manifest> </jar>
At the same time, java -jar MyProgram.jar will work correctly and will include all the libs JAR files in the class path.
Ian Roberts Apr 10 '13 at 4:07 a.m.
source share