Several running classes inside the JAR, how to run them?

I am having problems running several different classes from the same JAR file. I know that I can install one of the classes inside the JAR with the Main class, which will be launched after the java -jar myjar.jar , but I want something like:

 java -jar myjar.jar MyClass 

Is it possible to do this, or do I need to create several JARs (each for one run class), or is it better to create a class "manager" that will run my other classes by passing them command line arguments?
I was looking for documentation or a link, but I could not find it.

+42
java jar
Oct 20 '10 at 9:43
source share
7 answers

The format of the executable Jar file allows you to specify only one main class. In order for you to be able to run different applications, you need to either create a "manager", as you suggest, or use the classpath instead:

 java -cp myjar.jar MyClass 

However, this approach will ignore the class path that you configured in the Jar manifest file.

+52
Oct 20 '10 at 9:47
source share

you will need to use:

 java -cp myjar.jar MyClass 

and

 java -cp myjar.jar OtherMainClass 
+8
Oct 20 '10 at 9:45
source share

You do it like this:

 java -cp myjar.jar MyClass 

i.e. put the JAR in the classpath, then any class with the main method can be launched by specifying its full name. The -jar option exists only as a shortcut to use information in the JAR manifest (which may also include other JARs in the classpath, as well as specify the main class).

+4
Oct. 20 '10 at 9:45
source share

Wouldn't it be better for you to use the main "Launcher" class, whose function is to simply send calls to the actual controller classes and use the link file as the final wrapper instead of messing with the -cp wm option?

In windows, this is surprisingly easy to do.

The "main class" does not have to be something complicated, something like

 /** * Sample launcher */ public class Launcher { /** * @param args */ public static void main(String[] args) throws Exception { if (args != null && args.length > 0) { String option= args[0]; String[] args2=new String[0]; if( args.length>1){ args2= new String[args.length-1]; System.arraycopy(args, 1, args2, 0, args2.length); } if(option.equals("a")); new ClassA().exec(args2); else if(option.equals("b")); new ClassB().exec(args2); } } } 

On the side of the window of things, something like creating a link of this type is enough

 javaw.exe -jar "jarfile" "a" 

This is very useful for placing a link in the sendTo folder ... one bank, hidden, caused by many links that activate one of its aspects, simplifies the deployment of jar logic updates.

The actual selected files are passed as a list of lines after the parameters in the link definition.

Thus, you should not worry about the problems of the whole class.

+2
Oct 23 '14 at 16:27
source share

This doc will help you.

+1
Oct 20 '10 at 9:48
source share

Once the correct answer has been provided, there is a solution that you could use to create a jar of stubs for each main class with different manifests. This will allow you to create jar executables, allowing double-clicking for each individual program.

There are several ways to achieve this, but the basics should be that one class is similar to the next, which calls the intended main method, passing args.

 package com.acme.myapp; public final class Stub1 { public static void main(String[] args) { App1.main(args); } } 

As for packaging this, one way to use maven would be with maven-assembly-plugin : jar-with-dependencies mojo. The advantage here is that mojo will build a jar for the main main method, which is autonomous and does not need other assemblies in the classpath. He does this by copying the contents of each dependency ban into the resulting jar.

+1
02 Oct '14 at 6:55
source share

Jar files can contain only one Main-Class attribute in the manifest, which means that java -jar myjar.jar can run only one class.

You can run other runnable classes with

 java -cp myjar.jar OtherClass 

but this will not help users double-click the jar file.

Depending on how skilled your users are, perhaps for them the command line. If not, you can create a script for each runnable class or one script that takes arguments to select the correct class.

0
Oct. 20 '10 at 9:48
source share



All Articles