How to make jar files ???

Discuss all aspects of programming here.

Moderator: The Mod Squad

How to make jar files ???

Postby adroga » Sun Dec 26, 2004 7:52 pm

I want to know how to make a jar file.
I am able to make one if there is only one java file, but i am having trouble makeing a jar file from 3 classed that are related to eachother.

The three java files names are ...

I also have a pic that goes with this thats called Hospital.png

How would i do this??
Posts: 26
Joined: Wed Dec 31, 2003 10:22 pm

Postby olly » Mon Dec 27, 2004 11:13 pm

Just done this at school :wink: Here are my notes:

Using JAR Files

See: JAR Basics: your_J2SE_folder/docs/tooldocs/windows/jar.html
See: JAR Tutorial:

A useful technique for organizing class files is to combine them into a single file called a JAR or Java Archive. A JAR file is simply a ZIP compatible file containing a collection of java files (typically, class files, but any other kind of file could be included as well). You can pack a component or a complete application in a single file. Additionally, you can make an GUI application run direct from the JAR file without the console window.

You can combine multiple files into a single JAR archive file with the jar tool.

1. Create a JAR file

In MS Windows environment you run the jar tool on the command prompt:
Start → Run... → cmd.exe

Use the CD command to change the active folder.
For more details, type help cd and press Enter.

The following command,

C:\myfolder> jar -cvf myJarFile.jar abc\*.class

would add to myJarFile.jar all class files within the C:\myfolder\abc folder (the corresponding package name in the JAR file would be abc).
Jar options: c = create a new archive file, v = display verbose output, f = specify the JAR file name.

The following command,

C:\myfolder> jar -cvf util.jar abc\*.class C:\util\*.class

would add to util.jar all class files within the abc and C:\util folder. The corresponding package names in the JAR file would be abc and util.

The following command,

C:\myfolder> jar -cvf utilities.jar folder_1 folder_2

would add to utilities.jar all files and subfolders within the folders folder_1 and folder_2

You could also use a single argument file named "classes.list" to hold the names of the files:
C:\myfolder> dir /b *.class > myclasses.list

Or, if you also want to include class files in the subdirectories:
C:\myfolder> dir /b /s *.class > myclasses.list

Then execute the jar command passing in the argument file:
C:\myfolder> jar -cvf myJarFile.jar @myclasses.list

2. Make CLASSPATH to point to the JAR file

A jar archive can be used as a class path entry.

CLASSPATH: .;%CLASSPATH%;C:\myfolder\myJarFile.jar;

See the JAR documentation for other details.

JAR files in a special folder under current JRE

The Sun JVMs have a special folder (%JAVA_HOME%\jre\lib\ext) for class library "extensions". All JAR files present in this directory are automatically available to all applications using that JRE. No CLASSPATH entry is needed for your JAR file, if you put the JAR file in the following directory: C:\j2sdk1.4.2_04\jre\lib\ext
Green Belt
Green Belt
Posts: 222
Joined: Thu Jun 27, 2002 8:11 pm

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest