• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a jar in classpath on command line  RSS feed

 
Gilles Marceau
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

i am try to use a jar in the classpath on the command line and
it doesn't work. I have tried :
java -classpath testlib.jar -jar test.jar
where testlib.jar is a library jar needed to execute test.jar and test.jar
is a jar with a Main class described in its manifest.
Of course, both testlib.jar and test.jar are in the current directory.

On the other hand, putting testlib.jar in the ext/ directory inside the jre
and typing "java -jar test.jar" works well.

I really don't see why this doesn't work.
If any of you have a clue, let me know.

Gilles



Thanks for your help
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't work because it doesn't work.

When you use the "-jar" switch, the "-classpath" switch and the CLASSPATH environment variable are ignored. Executable jars are assumed to be self-contained.

Instead of saying

java -classpath X.jar -jar Y.jar

use something like

java -classpath X.jar;Y.jar com.something.MainClassFromYDotJar
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to what Ernest said:

The "-jar" switch is used to run executable JAR files. An executable JAR file contains a manifest file in which the main class and the classpath is specified. With the "-jar" switch, the -cp and -classpath options are ignored, and the classpath specified in the manifest file is used instead.

The Java Tutorial - Packaging Programs in JAR Files
 
Gilles Marceau
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much guys.

Gilles
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!