Run class in Jar file

jim picture jim · Jul 21, 2011 · Viewed 314.2k times · Source

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. Any help would be appreciated.

Answer

Bitmap picture Bitmap · Jul 21, 2011

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