0

Need to run particular java class from jar package through unix console. Is it possible? Thanks

johnny-b-goode
  • 3,792
  • 12
  • 45
  • 68

5 Answers5

4

It's not possible to call a specific method of a class from a terminal.

You can only call the main method with java -cp JarFile.jar package.ClassName

Or, if your jar contains a manifest file, then you can do: java -jar pJarFile.jar.

Mesop
  • 5,187
  • 4
  • 25
  • 42
2

Use

java -cp myJar.jar myClass

The -cp option is used when setting up the classpath manually ie give full path of location of jar file and then run command

Eg.

C:> java -cp C:\java\MyClasses\myJar.jar myClass
Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
2

For running a jar file you can have either of following approach. Both of them require that you know the fully qualified name of the class where main(String[] arg) method is written.

Say your class containing main method is com.myclass.MainClass

  1. You can run the jar directly. Keep the jar file at the location from where you are running this command

    java -cp yourjarfile.jar com.myclass.MainClass

  2. Create a manifest manifest.mf file with following content

    Main-class: com.myclass.MainClass

now create the jar file as follows

   jar -cmf manifest.mf yourjarfile.jar <your class file location> 

Run this jar with the following command

    java -jar yourjarfile.jar
Santosh
  • 17,667
  • 4
  • 54
  • 79
0

Yes, use the -jar parameter:

java -jar MyProgram.jar
Sjoerd
  • 74,049
  • 16
  • 131
  • 175
0

you can extract the specific class file by doing:

jar xf MyProgram.jar theclass.class      

it will end up in your current directory

then just run the classfile

java theclass
dunn less
  • 623
  • 10
  • 26