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.
-
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)
-
Is there a better way to setup the packages?
-
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)
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.
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.
It seems your package structure is okay no need to change it.
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.