Executable Jar Package

Source: Internet
Author: User

I have solved this problem, there is a packaging tool in eclipse that can package the program into a. jar file:
Right-click the project--->export--->java--->jar file--->next--->browse settings. The destination path to store the JAR file--->next---> Next---> Point on the main class side of browse, select the project's main classes,--->finish, OK, packaging is complete.

Recently studied how to summarize some of the issues encountered during Java Project packaging, such as packaging a Test.jar file

manifest-version:1.0
Main-class:windows. Videowindow
Class-path:lib\jetty-6.1.26.jar Lib\aspectjweaver-1.6.12.jar

The package description file is packaged as a jar above. The following error has been present.
Could not find main class. Program would exit
This error message is not actually found in the main class. It's the jar. This error can occur if an error is not executed. After packaging is complete, run cmd-java-jar Your.jar to confirm that there are no problems to execute the jar. After using the-jar parameter, the system's CLASSPATH variable no longer works. The virtual machine will go to MANIFEST.MF in the Class-path to find the relevant package.
Then you can succeed from: Windows. Videowindow's main method enters. Otherwise, there is no associated connection package, and you will be prompted to find the main class. And people may still be confused to find the north. Is there a main class?

If that's not the right thing to write. will always be the error.
Take a look at the above Class-path: the following notation. Separate spaces. There must be no semicolon. Remember.

Finally, there must be a line break. This is also to be noted. Otherwise the hint is not mainclass.

Don't delay your time because of this little thing. Where lib/indicates that the jar must be in a Lib subdirectory of the directory where the Test.jar is packaged.

command to run the jar: Java-jar Test.jar
Double-click can also, then use JAVAW to open.

About Java-classpath A.jar;b.jar; Class Test. When Test executes. A.jar;b.jar; There must be a semicolon behind them. Otherwise the error. Don't expect the system to look for itself without writing classpath. This is not in eclipse. You must write the class you want to use. Otherwise, you will always be prompted to compile an error.

2, MANIFEST. Odd Branch and space requirements in MF file

A very strange and interesting thing to find in the work, the formatting requirements for the branches and spaces in the MANIFEST.MF file are shared with everyone.

For the usual MANIFEST.MF file, the general format is:

Class-path:lib/a.jar Lib/b.jar Lib/c.jar Lib/d.jar Lib/e.jar Lib/f.jar
In one line, all the jar package paths are written, separated by spaces.

However, for some large-scale projects, because of the many dependencies, such as more than 30, if it is written in a row, there will be an amazing length of rows. There is no problem with the program running, but it is unfriendly to versioning, such as adding or decreasing a dependency package, which will be rewritten. Later compare different version, only know that this line has been modified can not directly know what is done to modify, must be compared by other means.

The same problem occurs when the code merge, if the two branches have modified the file, it must be done by hand to merge, and it is difficult and error-prone to compare what is changed to each other.

Therefore, an improvement is the dependency of this file in accordance with a one-way rewrite, so that later modification will only modify the line of dependency, it is easy to compare the specific what moved, code merge version control software is generally easy to directly automatically merge successfully.

The modified file resembles the following:

Class-path:lib/a.jar
Lib/b.jar
Lib/c.jar
Lib/d.jar
Lib/e.jar
Lib/f.jar

However, in the actual operation of unexpected problems occur, there will be exceptions or classes can not be found, after the inspection found that the problem appears in the MANIFEST.MF format, MANIFEST.MF for branches and spaces are special requirements:

1. Spaces are not allowed after the name of the last jar in each line
That is, "Lib/b.jar" after B.jar must enter the end of the bank, there can be no space, one can not

2. The beginning of each line must be no less than 2 spaces
That is, "Lib/b.jar" must have no more than two spaces before B.jar

The above two conditions there is a problem, a bit odd.

3. Strict MANIFEST.MF format
Today only found that the format requirements of this file is relatively strict, because the use of more than the package, so the content of the Class-path more, I put it all in a row, the server started to read the file when the error message, the wrong information is the line is too long, cut them into multiple lines, The result is an invalid header field (java.io.IOException:invalid headers field), and the other files in the project comparison, found after the line to be preceded by a space, and later found clearly contains a package, but can not find the class, A space is added to the back of each line, and this is completely done.

Executable Jar Package

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.