views:

29

answers:

2

I am new to Java and am making a license generator. This is my current setup.

com.example.licensegenerator.client (used by the client application)
   :LicenseLoader (no Main method)
   :LicenseDownloader (no Main method)

com.example.licensegenerator.server.keys (used by the server)
   :ProductKeyGenerator(Main method)

com.example.licensegenerator.server.license (used on the server also)
   :LicenseGenerator(Main method)

com.example.licensegenerator.lib (Shared classes between client and server)
    :Contants (no main)

Now I have a few questions.

  1. Is it OK to have multiple main() methods in a single project? Will I be able to compile them to different .jar files? (In this case I need two different jars for the server)

  2. Is there a better way to setup the packages?

  3. And a totally unrelated question, with exceptions, is it better to handle them right then and there or throw them and let the main method catch them all (the program cannot recover if an error occurs)

A: 

Here are the answers.

  1. It is okay if you have multiple classes with main methods. Only you need to do is to specify the class which executes first while executing.

    java [Starting class full qualified name]

Also, you can package the classes into multiple jar files. See the jar command structure at

http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/jar.html

In this, you can specify the classes you want to package into jar.

2.It seems your package structure is okay no need to change it.

3.Exception handling depends on the context. For example if it is known exception and if you want to give message to the user, better handle it in the class itself but if it is unknown exception, then throw it and handle it in main where you may want to print stack trace.

RaviG
A: 

When you're delivering your code (as JARs) you need to make sure that you deliver just the client code to the client. That might mean that it is best to put that in a separate module; a trick I often use is to put shared code in a shared module (which might or might or might not be multiple packages inside) and then have separate client and server modules that depend on the shared one. A tool like Maven makes this pretty easy to do.

And never define a package in multiple modules at once. That's just pain and confusion.

Donal Fellows