208

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

I thought it would be to go into the directory and say java -cp myJar.jar.myClass but that isn't working.

Francesco - FL
  • 603
  • 1
  • 4
  • 25
jim
  • 2,155
  • 2
  • 14
  • 6
  • `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
  • 1
    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? – Marsellus Wallace Jul 21 '11 at 18:17

5 Answers5

306

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

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
Bitmap
  • 12,402
  • 16
  • 64
  • 91
  • 12
    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 '14 at 11:48
  • 2
    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 C Wilson Sep 16 '14 at 23:43
  • 1
    use MyClass, not myClass – Breno Inojosa Jan 15 '16 at 21:10
  • By trying the above I am getting the error Exception in thread "main" java.lang.NoClassDefFoundError: cucumber/api/cli/Main at com.company.project.demo.bdd.runner.Execute.main(Execute.java:20) Caused by: java.lang.ClassNotFoundException: cucumber.api.cli.Main at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more – Vaibhav_Sharma Jul 11 '19 at 13:35
  • if you on linux use ':' instead of ';' for adding dependent jars, the classpath separator ':' is ';' on windows. example:-> java -cp dependentfile1.jar:dependentfile2.jar:c:\location_of_jar\myjar.jar com.mypackage.myClass – Dan Aug 04 '20 at 05:07
24

You want:

java -cp myJar.jar myClass

The Documentation gives the following example:

C:> java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool
Richard Campbell
  • 3,591
  • 23
  • 18
20

There are two types of JAR files available in Java:

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

  2. Simple jar file that does not contain a manifest file so you simply run your main class by giving its path java -cp ./fileName.jar MainClass

WhyGeeEx
  • 459
  • 1
  • 5
  • 19
Gaurav Singh
  • 209
  • 2
  • 2
7

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

Where:

  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.

1

This is the right way to execute a .jar, and whatever one class in that .jar should have main() and the following are the parameters to it :

java -DLB="uk" -DType="CLIENT_IND" -jar com.fbi.rrm.rrm-batchy-1.5.jar
AS Mackay
  • 2,831
  • 9
  • 19
  • 25
ubersucks
  • 11
  • 1