Need to run particular java class from jar package through unix console. Is it possible? Thanks
-
kindly check this http://stackoverflow.com/questions/6780678/run-class-in-jar-file – Milesh Oct 09 '12 at 13:34
5 Answers
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
.

- 5,187
- 4
- 25
- 42
-
@psed In that case, you have to do `java -cp JarFile.jar package.ClassName` with `package.ClassName` being the class containing the main method. – Mesop Oct 09 '12 at 13:37
-
-
-
1
-
I am getting could not find or load main class. I am running this in solaris – Julie Nov 17 '16 at 17:52
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

- 28,083
- 20
- 99
- 133
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
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
Create a manifest
manifest.mf
file with following contentMain-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

- 17,667
- 4
- 54
- 79
Yes, use the -jar parameter:
java -jar MyProgram.jar

- 74,049
- 16
- 131
- 175
-
-
Lets say its not a "MyProgram.jar" but "AllMyPrograms.jar" and i need to run "MyFirstProgram.java". – johnny-b-goode Oct 09 '12 at 13:33
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

- 623
- 10
- 26