211

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.

2
  • 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
    Commented Jul 21, 2011 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? Commented Jul 21, 2011 at 18:17

5 Answers 5

309

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

5
  • 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
    Commented Jul 16, 2014 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.) Commented Sep 16, 2014 at 23:43
  • 1
    use MyClass, not myClass Commented Jan 15, 2016 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 Commented Jul 11, 2019 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
    Commented Aug 4, 2020 at 5: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
21

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.