Take the 2-minute tour ×

if you have a jar file called myJar.jar in /myfolder and you want to use myClass from it, how do you go about doing it from the command line?

i thought it would be go into the directory and say java -cp myJar.jar.myClass but that isnt working. Any help would be appreciated

share|improve this question
java -cp myJar.jar myClass works fine for me -- do you have a spurious period in your command line instead of a space? –  Chris Dodd Jul 21 '11 at 18:17
What do you mean when you say that you want 'to use' that class? Is there a main method that you want to call in particular? –  Gevorg Jul 21 '11 at 18:17

4 Answers 4

up vote 74 down vote accepted

Use java -cp myjar.jar com.mypackage.myClass.

  1. If the class is not in a package then simply java -cp myjar.jar myClass.

  2. If you are not within the directory where myJar.jar is located, then you can do:

    1. On Unix or Linux platforms:

      java -cp /location_of_jar/myjar.jar com.mypackage.myClass

    2. On Windows:

      java -cp c:\location_of_jar\myjar.jar com.mypackage.myClass

share|improve this answer
Just adding to it - in case com.mypackage.myClass (in the above example) has dependencies on other jars, run it as follows: java -cp dependentfile1.jar;dependentfile2.jar;c:\location_of_jar\myjar.jar com.mypackage.myClass –  akjain Jul 16 at 11:48
A possible failure path to watch out for in case this doesn't work: be sure your fully qualified path to your class matches exactly what's in your jar file. If you do unzip -l /location-of-jar/myjar.jar, and see something other than com/mypackage/myClass, this is your problem. (For instance, if you see bin/com/mypackage/myClass, you should have cd'ed into bin to build your jar file; what you have won't work.) –  Scott Wilson Sep 16 at 23:43

You want:

java -cp myJar.jar myClass

The Documentation gives the following example:

C:> java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool
share|improve this answer

Their are two types of JAR file available in java

  1. Runnable/Executable jar file which contains manifest file. to run Runnable jar you can use Java -jar fileName.jar or java -jar -classpath abc.jar fileName.jar

  2. Simple jar file these does not contains manifest file so you simple run your main class by giving its path java -cp ./fileName.jar MainClass

share|improve this answer

Assuming you are in the directory where myJar.jar file is and that myClass has a public static void main() method on it:

You use the following command line:

java -cp ./myJar.jar myClass


  1. myJar.jar is in the current path, note that . isn't in the current path on most systems. A fully qualified path is preferred here as well.

  2. myClass is a fully qualified package path to the class, the example assumes that myClass is in the default package which is bad practice, if it is in a nested package it would be com.mycompany.mycode.myClass.

share|improve this answer

Your Answer


