I asked you to produce a .jar file for the Voca exercise just so that there was only one file VOCA.jar for the CourseMaster to load and we knew that your program should run if we type
java -jar VOCA.jar ourTestingFile.vocleaving you completely free to write as many class files as you want and also use additional resource files like images and sounds.
We also asked you to put all your source files in
another archive VOCA.tar so that the markers could actually see your code.
If your program does not use any resource files (e.g. images) you only need to read sections on making tar files and jar files. If you use resource files, read the section on accessing resources within jar files as well. Finally, if you are interested in putting applets in a jar, read the section on applets.
tar cvf VOCA.tar File1.java File2.java File3.java
tar tvf VOCA.tarTo unpack the tar file, type:
tar xvf VOCA.tarFor more in-depth information, type (on Unix):
man tar
java -jar VOCA.jar ourTestingFile.voc
Main-Class: Voca
jar cvmf manifest-addition VOCA.jar Voca.class GUI.class Question.class GUI$1.class GUI$2.class french.gif english.gif
jar tvf VOCA.jar
java -jar VOCA.jaror
java -jar VOCA.jar other.voc
ImageIcon fr = new ImageIcon("french.gif");the image is not loaded any more. The same holds for reading from default.voc if you bundled it in the VOCA.jar: the file is not found. Unfortunately you need to change your program.
ImageIcon fr = new ImageIcon("french.gif");with
URL url = MyClass.class.getResource("french.gif"); ImageIcon icon = new ImageIcon(url);where MyClass is the class which needs to load the resource (e.g. Voca, or GUI). Same for .au files.
For more in-depth information, read about the Class class
and getResource() method in Java 1.3 API.
Warning: apparently there are problems with using this
method in applets packaged as a jar file. Some versions of
Netscape consider it a security violation. There is also a
bug (again reported for Applets) in accessing resource files
placed in subdirectories rather than on the top level (e.g.
if you do
jar cvmf manifest-addition VOCA.jar classes/ images/
with all your class files in the classes/ directory and
all images in the images/ directory.)
InputStream in = Voca.class.getResourceAsStream("default.voc"); BufferedReader br = new BufferedReader(new InputStreamReader(in));
Note: apparently Netscape is happy with applets opening a file in the .jar archive using getResourceAsStream() rather than getResource().
Beware of the problems mentioned in italics in the previous section.<APPLET CODE="MyClass.class" ARCHIVE="MyJar.jar" WIDTH=500 HEIGHT=500> </APPLET>