Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute a main class from a jar

Tags:

java

jar

I have a jar which contain two main class Class A and Class B. In the manifest i have mentioned Class A . Now i have to execute classB from the same jar . what should be the command.

I dont prefer to make two separate jars.

Thanks

like image 734
harshit Avatar asked Feb 21 '11 19:02

harshit


People also ask

How do you call a main class from a jar?

To run an application in a nonexecutable JAR file, we have to use -cp option instead of -jar. We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …]

How do I run a Java main class?

Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.

How do I run a Java program from a jar file?

Running Jar file require you to have the jar file included in your class path. This can be done at run time using URLClassLoader . Simply construct a URLClassLoader with the jar as one of the URL. Then call its forClass(...) if you know the class name (full name of course).


1 Answers

This will do the job: java -classpath yourjar.jar youpackage.B

like image 134
Favonius Avatar answered Sep 20 '22 06:09

Favonius