tags:

views:

423

answers:

3

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.

A: 

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)

monojohnny
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
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
Ok, I think I understand in now.
Zach
A: 

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).

Carlos Heuberger
+1  A: 

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
trashgod
+1 most accurate answer
stacker