More than one "artifact"

This may be more a base Java question but I am not clear on how IntelliJ packages up a JAR file.

Currently I have a Java main class 'App' and I build an artifact that produces a .jar file that I run using 'java -jar app.jar'. This seems to work fine.

Now I want to create another Java main class for the sack of discussion I will call App2. I use IntelliJ to build another artifact that it seems to name app.App2. But when I look in the 'out' folder I still just see app.jar. My question is how do I change the java invocation to run this second Java main class? In other words 'java -jar app.jar' obviously needs to be ammended to indicate which of the 'main's will be executed. When I look at the MANIFEST that IntelliJ builds I see that the main class is the main inside of the original App class. Can this be overridded? Thank you for your help with this very basic question.


Please, explain how you create these jars. There are several possible ways to do it.


I create the .jar with Intellij. IDE. Project Stucture -> Artifacts -> Jar -> From modules with dependencies -> Copy to output directory and link via manifest (selected the only main available for the first. Select the "new" main for the second artifact).


When you create a Jar artifact with a Main-class specified IDEA generates a MANIFEST.MF file in your project directory and includes that file to the
jar. So if you want to create two jars with different main classes you need to choose different directory in 'Directory for META-INF/MANIFEST.MF'
field in 'Create Jar From Modules' dialog. Also you can change the 'Output Directory' field for the second artifact or change the name of the jar by
pressing Shift+F6 on the root node in 'Output Layout' tree to make sure that the second jar won't overwrite the first one.

Nikolay Chashnikov
Software Developer
JetBrains, Inc
"Develop with pleasure!"


Please sign in to leave a comment.