Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating jars with multiple main classes in netbeans

Tags:

java

jar

netbeans

I'm am creating a VoIP client server pair in java using the netbeans IDE. I would like to use only 1 project but I am unfamiliar with with how netbeans would create the Jar file. I need a to if this is possible and if so, how to set it up.

like image 275
Zach Avatar asked Feb 06 '10 01:02

Zach


People also ask

Can a jar have two main classes?

Jar files can contain only one Main-Class attribute in the manifest, which means a jar can contain only one mainClassName.

Can you have multiple main classes in Java?

From the above program, we can say that Java can have multiple main methods but with the concept of overloading. There should be only one main method with parameter as string[ ] arg.

How do you set the main class in a running 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 …] As we can see, in this case, we'll have to include the main class name in the command line, followed by arguments.


1 Answers

The JAR File Specification allows only one Main-Class attribute per JAR, but the JAR may have an arbitrary number of classes that declare a main() method. Any such class will be included in the project's Build Packaging property, unless specifically excluded.

As a concrete example, H2 Database includes these classes with main():

org.h2.jdbcx.JdbcConnectionPool
org.h2.tools.Backup
org.h2.tools.ChangeFileEncryption
org.h2.tools.Console
org.h2.tools.ConvertTraceFile
org.h2.tools.ConvertTraceFile
org.h2.tools.CreateCluster
org.h2.tools.DeleteDbFiles
org.h2.tools.Recover
org.h2.tools.Restore
org.h2.tools.RunScript
org.h2.tools.Script
org.h2.tools.Server
org.h2.tools.Shell

Addendum: Apparently, my junk-drawer project needs maintenance.

$ find scratch/src -name \*java | xargs -J % egrep 'main[ \t]*\(Str' % | wc -l
     109
like image 186
trashgod Avatar answered Oct 06 '22 19:10

trashgod