Multiple runnable classes inside JAR, how to run them?
The executable Jar file format only allows you to specify one main class. In order for you to be able to execute different applications, you'll need to either create a "manager" as you suggest, or to use the classpath instead:
java -cp myjar.jar MyClass
However, this approach will ignore the classpath you have configured in the Jar's manifest file.
Use a single external jar in multiple runnable jars
Technically it is possible, but you will need to update source code of all projects which use this single jar file.
Consider JCL Framework
Below, there's sample code from this comment
JarClassLoader jcl = new JarClassLoader();
jcl.add("myjar.jar"); //Load jar file
jcl.add(new URL("http://myserver.com/myjar.jar")); //Load jar from a URL
jcl.add(new FileInputStream("myotherjar.jar")); //Load jar file from stream
jcl.add("myclassfolder/"); //Load class folder
jcl.add("myjarlib/"); //Recursively load all jar files in the folder/sub-folder(s)
JclObjectFactory factory = JclObjectFactory.getInstance();
//Create object of loaded class
Object obj = factory.create(jcl,"mypackage.MyClass");
How to make a .jar file with multiple classes?
You can create an executable jar with multiple classes using Maven for packaging your classes. Then set the main class that serves as the application entry point using the MAVEN SHADE PLUGIN.
Read more about maven shade plugin here: [https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html][1]
Creating a single jar file for multiple jars each with a separate main class
You can only have oneach Main. So I would add an interface, IModule, say. Then each class implements them and you discover them through reflection.
How do I start multiple main programs in a Java executable .jar?
The jar manifest allows you to optionally specify no more than one main class. This is invoked when you execute java
with the -jar
flag.
java -jar myapp.jar
You may include multiple main classes in a single jar, but each (except the optional 1 above) must be invoked using the -classpath
flag and with the fully qualified name of the main class specified.
java -classpath myapp.jar com.mypackage.app.Main01 && \
java -classpath myapp.jar com.mypackage.app.Main02 && \
java -classpath myapp.jar com.mypackage.app.Main03
The example above will spawn three separate java VMs, each in their own process. Obviously, this does not meet your requirement for an 'executable jar'.
Alternatively, you may wish to have one main method that starts separate threads, so that there is only one process, but concurrent execution.
Ant is not a suitable choice to help you solve this issue. I suspect you probably want a single main method that spawns multiple threads. Feel free to provide more information on your requirements.
How to run a class from Jar which is not the Main-Class in its Manifest file
You can create your jar without Main-Class in its Manifest file. Then :
java -cp MyJar.jar com.mycomp.myproj.dir2.MainClass2 /home/myhome/datasource.properties /home/myhome/input.txt
jar-with-dependencies + Multiple mainClass classes?
Actually, it's perfectly fine for me to use a classpath where the fat jar contains everything:
java -cp myapp-with-dependencies.jar com.myapp.MyApp1
java -cp myapp-with-dependencies.jar com.myapp.MyApp2
Related Topics
Java8: Ambiguity with Lambdas and Overloaded Methods
Java.Lang.Outofmemoryerror: Permgen Space
How to Create Splash Screen with Transparent Background in Javafx
Sorting Strings That Contains Number in Java
Charsequence VS String in Java
Java Sax Parser Split Calls to Characters()
Java: Jprogressbar (Or Equivalent) in a Jtabbedpane Tab Title
How to Handle Print Dialog in Selenium
Java Best Practice for Date Manipulation/Storage for Geographically Diverse Users
How to Automate Drag & Drop Functionality Using Selenium Webdriver Java
Difference Between Running and Starting a Thread
Javabean Wrapping with Javafx Properties
Bitset to and from Integer/Long
Can Constructor Return a Null Object