Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to I access resources using Jar files

Using Jar Files

How to I access resources using Jar files

by  ackka  Posted    (Edited  )
This is a simple FAQ on how to use .jar files in Java(Sun) Applets..

by tom

Jar files are basically the same thing as a .zip file. You can put anyfile that you want inside, .gif .class .java etc.. The reason they designed .jar files is so your Applet does not have to make multiple TCP/IP connection for a file. For example say you have a simple animation program, each Image() variable you create has to make a TCP/IP connection to the Internet, download the file, then display it. The same if you are reading text files, even the .class files are downloaded seperately. By using .jar files you bundle all these files into one file, that is downloaded once. Using .jar files is a little more difficult, accessing the resources is different for each medium.

How to create Jar files:

In the JDK there is a utility called jar.exe that creates these files. Has a variety of options...................
C:\work\jar faq>jar
Usage: jar {ctxu}[vfm0M] [jar-file] [manifest-file] [-C dir] files ...
Options:
-c create new archive
-t list table of contents for archive
-x extract named (or all) files from archive
-u update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information from specified manifest file
-0 store only; use no ZIP compression
-M Do not create a manifest file for the entries
-C change to the specified directory and include the following file
If any file is a directory then it is processed recursively.
The manifest file name and the archive file name needs to be specified
in the same order the 'm' and 'f' flags are specified.

Example 1: to archive two class files into an archive called classes.jar:
jar cvf classes.jar Foo.class Bar.class
Example 2: use an existing manifest file 'mymanifest' and archive all the
files in the foo/ directory into 'classes.jar':
jar cvfm classes.jar mymanifest -C foo/ .

How to use Jar files

Your html file must include the following

<HTML>
<APPLET CODE = "JarTest.class" archive = "JarTest.jar" WIDTH = 500 HEIGHT = 500 >
</APPLET>
</body>
</html>

or to force the browser to use the Java Plugin
<HTML>
<!--"CONVERTED_APPLET"-->
<!-- CONVERTER VERSION 1.3 -->
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
WIDTH = 500 HEIGHT = 500 codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">
<PARAM NAME = CODE VALUE = "JarTest.class" >
<PARAM NAME = ARCHIVE VALUE = "JarTest.jar" >

<PARAM NAME="type" VALUE="application/x-java-applet;version=1.3">
<PARAM NAME="scriptable" VALUE="false">
<COMMENT>
<EMBED type="application/x-java-applet;version=1.3" CODE = "JarTest.class" ARCHIVE = "JarTest.jar" WIDTH = 500 HEIGHT = 500 scriptable=false pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html"><NOEMBED></COMMENT>

</NOEMBED></EMBED>
</OBJECT>

<!--
<APPLET CODE = "JarTest.class" ARCHIVE = "JarTest.jar" WIDTH = 500 HEIGHT = 500>


</APPLET>
-->
<!--"END_CONVERTED_APPLET"-->


</body>

</html>

And here is the sample code how to grab a .gif and .txt file and link it to your Java program, i wrote this as a applet.......

I have also posted this on my website at so you can just copy it.......
http://www.angelfire.com/electronic/ackka/java/jarfaq/

Code:
//this is a simple java applet that shows how to load differing resources from a .jar file, 
//this is showing how to load an image and a text file....
//The image is put on the screen
//The text file is dumped to the system console
//Even the .class file is included in the .jar file

import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.lang.*;
import java.io.*;



public class JarTest extends Applet 
{

Image sample;


public void init()
{

sample = getImage("arrowdown.gif");

try
	{

	InputStream in = JarTest.class.getResourceAsStream("JarTest.txt");
	BufferedReader br =new BufferedReader(new InputStreamReader(in));
	String line;
	
	while( (line = br.readLine() ) !=null)
		System.out.println(line);
		//dumps the contents of the data file to the System Console

	}
catch(IOException e)
	{
	System.out.print(e + "This error has occurred \n");
	}



}


public void paint(Graphics screen)
{

screen.drawImage(sample,0,0,this);

//This code is from CoreJava Fundementals



}

//from http://dev-gamelan.earthweb.com/journal/techworkshop/121197_jar.html

public Image getImage(String imageFileName) 
{
URL imageURL = getClass().getResource(imageFileName);

if (imageURL == null) 
{
// or otherwise handle the error	
System.out.println("No image named" + imageFileName);
return null;
}
Toolkit tk = Toolkit.getDefaultToolkit();
		
Image img = null;
try 
	{
	img = tk.createImage( (java.awt.image.ImageProducer) imageURL.getContent());
	}
catch (java.io.IOException ex) 
	{
	System.out.println(ex);
	}
return img;

}

}

Other Related Links
http://dev-gamelan.earthweb.com/journal/techworkshop/121197_jar.html

Good luck and if you have any question my email is
thomas.moses@gmail.com
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top