Skip to content
Advertisement

How to run a java class with a jar in the classpath?

So, I can do this very well:

java mypackage.MyClass

if ./mypackage/MyClass.class exists. I can also happily do this:

java -cp myjar.jar mypackage.MyClass

if the class file exists in the appropriate part of the jar. Easy stuff. But I can’t for the life of me manage to do something like this:

java -cp utilities.jar mypackage.MyClass

where ./mypackage/MyClass.class exists, and where ./utilities.jar exists (not containing MyClass, of course).

Am I about to feel stupid?

Advertisement

Answer

Possibly 🙂

# On Unix
java -cp utilities.jar:. mypackage.MyClass

# On Windows
java -cp utilities.jar;. mypackage.MyClass

Basically that’s just including . (the current directory) on the classpath as well as the jar file.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement