Use the jar command to package the Java class file and compile a batch file that is convenient to execute the JAR file.

Source: Internet
Author: User

1. Package jar files
Assume that the file to be packaged is as follows:
Classes/COM/Albert tsong/MyApp. Class
Classes/COM/Albert tsong/AAAA. Class
Classes/COM/Albert tsong/util/Bbbb. Class
(MyApp contains the main function)
Create a mymanifest. MF file with the following content:
Main-class: COM. Albert tsong. MyApp

Create a myjar. BAT file with the following content:
Set java_home = C:/jsdk1.5.0 _ 09
Set Path = % java_home %/bin
Set classpath = % java_home %/lib/tools. jar;
Jar cvfm MyApp. Jar mymanifest. mf-C classes.

Both files are put in the same directory as classes, that is, the directory structure is the same as below
MyApp/classes/COM/Albert tsong/MyApp. Class
MyApp/classes/COM/Albert tsong/AAAA. Class
MyApp/classes/COM/Albert tsong/util/Bbbb. Class
MyApp/mymanifest. MF
MyApp/myjar. bat
Then click myjar. BAT to execute the file :)

2. Execute the JAR File
Create a directory named MyApp/release.
Copy the generated MyApp. jar file to this directory.
Create a new batch file run. BAT in this directory. The content is as follows:
Set java_home = C:/jsdk1.5.0 _ 09
Set Path = % java_home %/bin
Set classpath = % java_home %/lib/tools. jar;
Javaw-jar MyApp. Jar

3. Others
1) jar cvfm MyApp. Jar mymanifest. mf-C classes.
In this line of command, the last. Must not be dropped. The example of help provided by the jar command is
Jar cvfm classes. Jar mymanifest-C Foo /.
But in Windows, it should be changed
Jar cvfm classes. Jar mymanifest-C Foo /.
Or
Jar cvfm classes. Jar mymanifest-C Foo.
2) If the application is large, other libraries may be used. In this case, you can create a lib under the release directory.
Then add the library used in the classpath of run. bat.
Set classpath = % java_home %/lib/tools. jar; LIB/ccc. jar; LIB/DDD. jar;
3) If you want to specify the memory used by the Java program
Javaw-ms100m-mx256m-jar MyApp. Jar
The minimum memory size is MB and the maximum memory size is MB.
4) if the main function requires Parameters
Javaw-ms100m-mx256m-jar MyApp. Jar arg1 arg2
5) if you cannot see the relevant information on the console, you can use Java to execute the jar package.
Java-ms100m-mx256m-jar MyApp. Jar arg1 arg2
Javaw is executed in the background.
6. If the batchfile flash and the output is not displayed, you can add pausein the last line of the file. The best thing is to run. bat through cmd.exe.

Postscript:
I wrote one earlier and later lost it. Today, I forgot the dot and backslash, which wastes a lot of time.

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.