Creating jars with multiple main classes in netbeans?

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.

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 rather than having to specify the whole package/class name like: java -cp 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).

The reason for 2 main classes is because of the server and client. I would like to have a jar file created for each main class while contained in one project. Maybe this clarified my question.

– Zach Feb 6 '10 at 1:42 So you want one project and two Jars I think: so I think in that case you probably need to go for a 'free-form project': so you can customise the build (ant) scripts to do this. I usually just work on two projects in that scenario : unless you really need to share a lot of code between the two projects: then consider creating a third 'library' project - you can then add that as a dependant library to the other two. I hope that makes sense!

– monojohnny Feb 6 '10 at 1:54 Ok, I think I understand in now. – Zach Feb 6 '10 at 1:56.

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.

1 most accurate answer – stacker Feb 7 '10 at 8:38.

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 cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions