Java program package into a jar package

Source: Internet
Author: User

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.

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.