This is the directory structure of my java project in Eclipse IDE.
src
|_ com.san.app
|_LoggerServer.java ---> main() is defined here.
|_NetworkFileLogger.java
|_com.san.logger
|_Logger.java
|_LoggerImpl.java
|_LoggerFactory.java
|_SanLoggerException.java
|_com.san.network
|_NetworkListener.java
|_NetworkListenerImpl.java
|_NetworkListenerFactory.java
|_SanNetworkException.java
The directory structure is shown. A sanpshot from my Eclipse IDE.
Assuming all your class files are in the same directory structure as your source files. Snapshot of my directory structure.
bin
|_ com
|_san
|_app
|_LoggerServer.class
|_NetworkFileLogger.class (etc)
|_logger
|_network
Now assuming that you are at the top level directory bin of your class files. Create a new file MANIFEST.MF in the bin directory as follows using your favorite text editor. I used gvim for windows.
Notice the marking. It is going to indicate the class which has main() defined among all the classes and the class file to run when the jar file is executed. I have added the Main-Class property.Now let us create the jar file.This is how your directory structure should look like now. bin directory looks like below.
Now, create the jar file by including the Manifest.mf file which we created using the following command.
jar -cvfm LoggerServer.jar MANIFEST.MF com
Let me explain the jar command. jar command is similar to tar command used generally on UNIX like systems . It creates an archive. In this case it says - create an archive be verbose, name the file LoggerServer.jar and include the manifest file which is MANIFEST.MF , include all the class files under the com directory and it's sub directories while creating this jar.
'c' - create new archive
'v' - verbose
'f' - file name - LoggerServe.jar in this case.
'm' - include manifest information from specified manifest file.The ServerLogger.jar is now executable from the command line as follows :
java -jar LoggerServer.jar
This is possible only because we have added a MANIFEST.MF file indicates which is the main class in the jar.
Any arguments can be passed to main as below :
java -jar LoggerServer.jar 8888
That's it. Hope this blog helped someone somewhere create their first executable jar.