开发者

Creating jars with multiple main classes in netbeans

开发者 https://www.devze.com 2022-12-19 03:11 出处:网络
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

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 possib开发者_StackOverflow社区le and if so, how to set it up.


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


I don't know how Netbeans work, but it should be no problem putting more than one main class in a JAR. Actually a main class is just a class having a main method and a JAR is a collection of class files.
The only restriction is that there can only be one class that will be started with double-clicking the JAR.

To start the each class you must not use the -jar option, but provide the full class name.
For example, if you have a Client and a Server class in your JAR, the Client is started by
java -cp file.jar Client
and the Server by
java -cp file.jar Server.

An option is to create a third starter class used to start either the server or the client based on a command line argument (or a GUI window).


I believe the purpose of a 'main class' means that the eventual JAR' manifest file, will label one class as being the 'class to run': so that the end-user can just double-click it* or run a simplified commandline like:

java -jar <jar>

rather than having to specify the whole package/class name like:

java -cp <jar> com.yourcom.package.classname

If I'm right, then I don't see how it would make sense to have more than one main class ? Maybe I misunderstod your question - or there is another purpose to the 'main' class?

If you mean having two classes which have a 'main' method - then this is fine - the end user can launch any of the classes by name - and so long as they have the standard main method sig, for instance:

public static void main(String[] args)

it should just work.

*(on Windows at least, and whether that works also depends on which JRE they have and probably other things)


In netbeans, if you want to create jar file of your project with your selected main class then , 1. right click on your project (If you couldn't find your project then go to window -> Project from netbeans menubar ) 2. Go to set configuration -> Customize. 3. Select run from categories, In main class write your packagename.classname (ie you have created file xyz in mypackage, then write mypackage.xyz) 4. click ok.
5. Again right click on projectname, select clean and build. 6. Your jar file is created at location of your projectname -> dist -> projectname.jar

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号