Showing posts with label jar. Show all posts
Showing posts with label jar. Show all posts

Monday, 20 June 2011

How to make an executeable jar

Delivering desktop application developed in java, its very interactive that you give your software in a executable jar, rather giving .bat files (running java <main>.class)
To do that, try these steps.
First, jar all of your class files and let Java create it’s own manifest. First, make sure you are in the directory of your class files:
jar cvf MyJarFile.jar *.class
You would replace MyJarFile.jar with whatever you want the jar file to be named. Then, create a regular text file with this:
Main-Class: MainClass
Make sure you hit enter at the end. The file has to end with a carriage return. Say you name the text file ManifestUpdate.txt. Make sure you save it in the same directory as all your class files.
Then do this:
jar umf ManifestFile.txt MyJarFile.jar
This essentially updates the current Manifest file in your .jar file with the contents of the text file you created.
Again, go through the JAR tutorial. It will help you out a lot!
http://java.sun.com/docs/books/tutorial/deployment/jar/

Saturday, 11 June 2011

Find from which jar a class was loaded

In order to find at runtime where from file system a class was loaded, following command would be useful:
System.out.println(<myjavaclassname>
.class.getProtectionDomain()
.getCodeSource().getLocation());

Output will be something like this:
file:/C:/TestWorkSpace/lib/MyLibrary.jar

It can be helpful while debugging some class loading problems.

Saturday, 14 May 2011

Difference between JAR, WAR and EAR

Acronyms

jar - Java archive (file with .jar extension) [For simple java application]
war - Web archive (file with .war extension) [For simple java + jsp/servlet]
ear - Enterprise archive (file with .ear extension) [For simple java + jsp/servlet + EJB]
All three files are zipped file system and used for different purpose.

Understanding them one by one
JAR : JAR is a platform-independent file format that aggregates many files into one. Typically a JAR file contains the class files and auxiliary resources like libraries files, properties file, image, sound etc.
WAR : It is used to deployed in web application like Tomcat. It typically contains servlets, JSPs and their supporting classes and files. A servlet container like Tomcat is required to deploy these file. There are special files and directories within a WAR file. A WAR has a specific directory structure. The top-level directory of a WAR is the document root (WEB-INF) of the application. The document root is where JSP pages, client-side classes and archives, and static Web resources are stored. WEB-INF contains web.xml, classes, lib and Tag library descriptor files.
EAR : An EAR file is a standard JAR file with a .ear extension which is a generally J2EE application. It contains one or more entries representing the modules of the application, and a metadata directory called META-INF which contains one or more deployment descriptors. It is a top-level container which contains modules like: EJB modules, web modules, application client modules etc and deployed to an application server like WebLogic, WebSphere, JBOSS, etc. It might contain WARs, JARs, etc.
JAR -> WAR -> EAR
EAR = WAR(Web module) + JAR(can be EJB module or application client module)

Wednesday, 20 April 2011

Path and classpath in java

For running a simple program we need to set the java path on command prompt(for temporary )& in Environment variable using PATH variable & CLASSPATH variable :

PATH variable 
In JDK the PATH variable contains directories where binary files (e.g. EXE files in Windows) will be looked for.We set the PATH variables like this i.e path C:\Java\jdk1.6.0_03\bin 

(i) On command prompt
C:\>set path=%path;C:\Java\jdk1.6.0_03\bin%
When you open a command prompt and type "javac", you're supposed to have the "bin" directory of your sdk into the PATH, otherwise you'll get an infamous "Command not found" error message.

CLASSPATH 
In JDK the CLASSPATH contains directories (or JAR files), from where your java compiler/runtime will look for .class files (and some others). For example, "java Hello.class" will not work unless you set the directory (or JAR file) Hello.class is in, into your CLASSPATH.
i.e.classpath  C:\Java\jdk1.6.0_03\lib
For setting CLASSPATH using command prompt  Java class path can be set using either the -classpath option when calling an SDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.

On command prompt 
C:\>set classpath=%classpath;C:\Java\jdk1.6.0_03\lib%

JARs on the classpath
Java compiler and run-time can search for classes not only in separate files, but also in `JAR' archives. A JAR file can maintain its own directory structure, and Java follows exactly the same rules as for searching in ordinary directories. Specifically, `directory name = package name'. Because a JAR is itself a directory, to include a JAR file in the class search path, the path must reference the JAR itself, not the directory that contains the JAR. This is a very common error. Suppose I have a JAR jarclasses.jar in directory /jarclasses. The Java compiler look for classes in this jar, we need to specify: javac -classpath /jarclasses/jarclasses.jar ...
and not merely the directory jarclasses.  

For the CLASSPATH  use  CLASSPATH Environment Variable 
In java programming language we use the following packages such as  java.io.*;  java. util.*;These  packages are  just a set of classes, you will want to store them in one place, separate from the directory where you are developing your program. Sometimes these libraries have a set of files stored in a single jar file ( with a .jar extension). From where  the Java compiler and run programs must be able to find any of these. This is done for command line use of Java and for some editors and IDEs by setting an environment variable called CLASSPATH. For  Windows, the CLASSPATH environment variable should look like this :
c:\MyJavaLib;c:\MyJavaLib\myutils.jar;c:\MyJavaLib\blackboxclasses.jar;.
At the end " . "  includes the current directory in the search for classes.
  • Suppose we have a directory MyJavaLib on the C-drive that contains some utility classes or the directories that correspond to some packages. This  is the part before the first semi-colon.
               
  • Second part indicates that we have some classes stored in a file myutils.jar
             
  • Third part indicates that we have a jar file blackboxclasses.jar. 
Finally, after the last semi-colon we have the period( . ), indicating that the current directory is on the CLASSPATH. If some things are stored in directories that are subdirectories, the complete path, starting with "c:\" should be in the CLASSPATH.

Setting classpath in windows 7,Vista and xp graphically

Monday, 11 April 2011

Convert Jar file to EXE executable.

It is sometime desirable to convert the JAR file in to EXE (executable) and distribute the EXE. Although the exe can be executed only in Windows environment, this will definitely affects the “platform independency” of a Java program. Still if you want to convert a JAR file in EXE then following tools are for you.

JSmooth .exe wrapper

JSmooth is a Java Executable Wrapper. It creates native Windows launchers (standard .exe) for your java applications. It makes java deployment much smoother and user-friendly, as it is able to find any installed Java VM by itself. When no VM is available, the wrapper can automatically download and install a suitable JVM, or simply display a message or redirect the user to a web site.
JSmooth provides a variety of wrappers for your java application, each of them having their own behaviour: Choose your flavour!
Download: http://jsmooth.sourceforge.net/

JarToExe 1.8

Jar2Exe is a tool to convert jar files into exe files.
Following are the main features as describe in their website:
-Can generate “Console”, “Windows GUI”, “Windows Service” three types of exe files.
-Generated exe files can add program icons and version information.
-Generated exe files can encrypt and protect java programs, no temporary files will be generated when program runs.
-Generated exe files provide system tray icon support.
-Generated exe files provide record system event log support.
-Generated windows service exe files are able to install/uninstall itself, and support service pause/continue.
-New release of x64 version, can create 64 bits executives. (May 18, 2008)
-Both wizard mode and command line mode supported. (May 18, 2008)
Download: http://www.brothersoft.com/jartoexe-75019.html

Executor

Package your Java application as a jar, and Executor will turn the jar into a Windows exe file, indistinguishable from a native application. Simply double-clicking the exe file will invoke the Java Runtime Environment and launch your application.
Download: http://mpowers.net/executor/

Advanced Installer

Advanced Installer lets you create Windows MSI installs in minutes. This also has Windows Vista support and also helps to create MSI packages in other languages.
Download: http://www.advancedinstaller.com/
Let me know other tools that you have used to convert JAR to EXE. And also comments/reviews about these tools.

Thursday, 7 April 2011

Create 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

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

View contents of a JAR file

jar tf JAR_FILE_NAME
e.g.
jar tf MyApp1.jar

View contents with detail of a JAR file


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

Extract content of JAR file

jar xf JAR_FILE_NAME
e.g.
jar xf MyApp1.jar

Extract specific file from JAR file

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

Update a JAR file


jar uf JAR_FILE_NAME FILE_NAMES_FROM_JAR_FILE
e.g.
jar uf MyApp1.jar Test1.class

Executing a JAR file


java -jar JAR_FILE_NAME
e.g.
java -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.

Manifest-Version: 1.0
Main-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.

jar cvfm MyApp.jar MANIFEST.MF FILE_NAMES_OR_DIRECTORY_NAME