Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

08 March, 2010

Create JAR file in Java & Eclipse

Let us see how to create a JAR file using Java’s jar command as well as using Eclipse IDE. The JAR file format is based on the popular ZIP file format. Usually these file are used for archiving and distribution the files and implementing various libraries, components and plug-ins in java applications. Compiler and JVMs (Java Virtual Machine) can understand and implement these formats for java application.

JAR file using Java commands

Following are few commands that can be used to create/view/modify/execute a JAR file using Java command line utilities and JVM.

Create a JAR file

1jar cf JAR_FILE_NAME FILE_NAMES_OR_DIRECTORY_NAME
2e.g.
3jar cf MyApp1.jar C:\JavaProject\MyApp

View contents of a JAR file

1jar tf JAR_FILE_NAME
2e.g.
3jar tf MyApp1.jar

View contents with detail of a JAR file

1jar tvf JAR_FILE_NAME
2e.g.
3jar tvf MyApp1.jar
Note that we have used v (verbose) option to see the detail of JAR.

Extract content of JAR file

1jar xf JAR_FILE_NAME
2e.g.
3jar xf MyApp1.jar

Extract specific file from JAR file

1jar xf JAR_FILE_NAME FILE_NAME(S)_FROM_JAR_FILE
2e.g.
3jar xf MyApp1.jar Test1.class

Update a JAR file

1jar uf JAR_FILE_NAME FILE_NAMES_FROM_JAR_FILE
2e.g.
3jar uf MyApp1.jar Test1.class

Executing a JAR file

1java -jar JAR_FILE_NAME
2e.g.
3java -jar MyApp.jar

Create an executable JAR file

In order to create an executable JAR, one of the classes that we include in our JAR must be a main class.
Create a text file called MANIFEST.MF using any text editor and copy following content in it.
1Manifest-Version: 1.0
2Main-Class: MyMainClass
Where MyMainClass is the name of the class that contents main method. Also note that you have to specify fully qualified class name here.
Use following command to create an executable JAR file.
1jar cvfm MyApp.jar MANIFEST.MF FILE_NAMES_OR_DIRECTORY_NAME

JAR file using Eclipse IDE

Creating JAR file using Eclipse IDE is pretty much easy. Follow the simple steps.
Right click on your project, which you want to create a JAR file of. And select Export from the context menu.

Select JAR file from Java folder and click Next.

Provide the Destination path and click on Finish to create the JAR.

Courtesy: Viral Patel
Author: Viral Patel

20 February, 2010

Hide Closed Projects in Eclipse Project Explorer View

Ever wanted to hide all those closed projects in eclipse? Well, here is a simple trick that you may not know already.
Imagine your Eclipse workspace is filled with many projects that you created in past just to test some small functionality. Now you are done with these projects and hence you have closed it. Eclipse by default show all the closed projects in Project Explorer. Hence your project explorer may look like following where only two projects are open and rest competing for your attention although they are closed!

eclipse-close-project

Eclipse comes with a cool tiny feature that many of us not know. You may want to Hide all those closed projects from your workspace in Project Explorer tab. Simply follow following steps and do this is few seconds!
Step 1: Click on right corner of Project Explorer tab and open context menu. Select Filters option from menu.
eclipse-project-explorer-filter
Step 2: Check Closed Projects option from Filter and press Ok
eclipse-filter-close
Now check your project explorer tab, all the closed projects are gone! Doesn’t it looks more clean :)
We used Filters option to remove closed projects from Project Explorer tab. Filters provides more options to select and remove from displaying it from explorer view.

Courtesy: Viral Patel
Author: Viral Patel