for example, there is a Program . In package "cn. Jar. Test", the program has two resource files, about.txtand about.gif:
Package CN. jar. test; <br/> Import Java. AWT. *; <br/> Import Java. io. *; <br/> Import java.net. *; <br/> Import Java. util. *; <br/> Import javax. swing. *; </P> <p> public class resourcetest {<br/> Public static void main (string [] ARGs) {<br/> eventqueue. invokelater (New runnable () {<br/> Public void run () {<br/> resourcetestframe frame = new resourcetestframe (); <br/> frame. setdefaclocloseoperation (jframe. exit_on_close); <br/> frame. setvisible (true); <br/>}< br/> }); <br/>}</P> <p> @ suppresswarnings ("serial ") <br/> class resourcetestframe extends jframe {<br/> Public resourcetestframe () {<br/> settitle ("resourcetest"); <br/> setsize (default_width, default_height ); <br/> URL abouturl = getclass (). getresource ("about.gif"); <br/> image IMG = toolkit. getdefatooltoolkit (). getimage (abouturl); <br/> seticonimage (IMG); </P> <p> jtextarea textarea = new jtextarea (); <br/> inputstream stream = getclass (). getresourceasstream ("about.txt"); <br/> consumed in = new Queue (Stream); <br/> while (in. hasnext () {<br/> textarea. append (in. nextline () + "/N"); <br/>}< br/> Add (textarea ); <br/>}< br/> Public static final int default_width = 300; <br/> Public static final int default_height = 300; <br/>}
The directory structure is as follows:
Step 1: switch to the ../src/CN/JAR/test directory and compile it with javac:
Javac resourcetest. Java
Step 2: switch to the ../src directory, run the program, and check for any running errors:
Java CN. Jar. Test. resourcetest
Step 3: Copy all .class files and resource files about.txt and about.gif to another test directory, such as C:/src/CN/JAR/test. The same directory structure is cN/JAR/test.
Step 4: generate the jar package and switch to C:/src:
Jar CVF test. Jar CN
Step 5: add the configuration file: Create the test. MF file in the C:/src directory, open it in notepad, and add the following lines. Note that you must wrap the line after entering the file:
Main-class: CN. Test. Jar. resourcetest
Step 6: update the default configuration file:
Jar Umf test. MF test. Jar
So a jar package is generated and packaged. You can copy the test. jar package to another directory, such as C:/temp, and run
Java-jar test. Jar
To test whether the package is successful.