Thursday, July 22, 2010

HOWTO - Create an executable jar file.

This blog explains how to create an executable jar file.I have often run my java programs using the dot separated package structure. For example : java com.devicemantra.apps.blueapp.Main, it looked akward and complicated. Wanted to run it like  java -jar blueapp.jar.  Finally had some time to check this out. It is a simple process and involves creating a Manifest file and familiarity with the jar command. Both are simple.


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.


Wednesday, July 7, 2010

Java: Enhanced for loop and Array puzzle.

I have done a lot of programming related puzzles as a student and professional for fun or for taking tests. But this seemingly innocent and straightforward looking piece of code stumped me !  I had to actually try this out in Eclipse running in debug mode to Watch the variables. Now when i figured what is happening it looks straightforward.

The reason why i could not figure this out is first of all  :
1) It looked straightforward ( overconfidence stepped in )
2) I was used to the for(int counter = 0; counter< ...; counter++)  way of writing for loops and indexing arrays.

Whatever said, this one was a good puzzle. And i will NOT forget this one all my life :) ! 

public class Main {
   
    public static void main(String... pArgv) {
        int []arr = {1,2,3,4};
        for ( int i : arr )
        {
           arr[i] = 0;
        }
        for ( int i : arr )
        {
           System.out.print(i);
        }
    }
}


By the way the output is : 0030

Followers

About Me

I'm a software developer with interests in Design Patterns, Distributed programming, Big Data, Machine Learning and anything which excites me. I like to prototype new ideas and always on the lookout for tools which help me get the job done faster. Currently, i'm loving node.js + Mongodb.