INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Using Jar Files

How to I access resources using Jar files by ackka
Posted: 7 Jan 01 (Edited 29 Apr 05)

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 = &quot;JarTest.class&quot; archive = &quot;JarTest.jar&quot; WIDTH = 500 HEIGHT = 500 >
</APPLET>
</body>
</html>

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

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

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

<!--
<APPLET CODE = &quot;JarTest.class&quot; ARCHIVE = &quot;JarTest.jar&quot; WIDTH = 500 HEIGHT = 500>


</APPLET>
-->
<!--&quot;END_CONVERTED_APPLET&quot;-->


</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(&quot;arrowdown.gif&quot;);

try
    {

    InputStream in = JarTest.class.getResourceAsStream(&quot;JarTest.txt&quot;);
    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 + &quot;This error has occurred \n&quot;);
    }



}


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(&quot;No image named&quot; + 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

Back to Java FAQ Index
Back to Java Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close