Package Eclipse project into jar

Source: Internet
Author: User

Question about how eclipse packages Java app projects into jar

How can I package the written Java code? Because many third-party Lib is used in the source code, it is necessary to package the Lib together. He uses the export function of eclipse to directly package it into the. jar file. The third-party Lib is packaged in, but he still prompts that the corresponding package cannot be found during use.

In fact, it is not a proper practice to package lib released by others into the package of your project for your convenience. Before doing so, check the license of the other party, but whether or not to include the other party's lib, since your code is used, in order to make them work normally, specify which packages are essential. The instructor actually needs a manifest. MF file. When using eclipse to generate a jar file, do not let it automatically generate manifest. MF. Instead, you should write a manifest. MF file by yourself and then tell
Eclipse: use this file. For details, please read Sun's specification (VIA) or the JAR file on the IBM website. I will not write more here.

In addition, eclipse also provides an executable jar function, which can help you compile appropriate manifest. mf file to help you package the third-party lib you referenced (pay attention to license ).

First, export the jar package of the common class. The common class refers to the class that contains the main method and does not use other jar packages.

1. In eclipse, select the class or package you want to export, right-click and select the export sub-option;

2. In the pop-up dialog box, select Java file --- Select JAR file and click Next;

3. select the location and name of the jar package you want to generate in the text box behind the JAR file. Note that you should check the export generated class files and resources and export Java source files and resources, click Next;

4. Click the next button twice to reach the jar manifest specification. Note: select the entry class of your jar package in the text box next to main class at the bottom. Click Finish.

In the DOS environment, you can enter the directory where your jar is located and run Java-jar name. jar to check whether the operation is correct.

Second, other jar packages are used in the class you want to export. For example, the class you write connects to the database and uses the database driver package CMDL. jar ..

1. First, export the class you want to export to form a jar package, such as test. jar.

2. Create a folder main, for example, under the root directory of drive D;

3. Copy test. jar and CMDL. jar to the main file, right-click test. jar, and decompress it to the current folder. Cut META-INF \ manifest. MF to another place (such as desktop !) ;

4. Right-click CMDL. jar and decompress it to the current folder.

5. In the DOS environment, go to the main folder of the d disk and execute jar cvfm new. Jar meta-INF/manifest. MF.. Do not forget the last point.

6. Use the compression tool to open your New. jar and overwrite the original New. jar with the META-INF \ manifest. MF you put on the desktop.

In the DOS environment, you can enter the directory where your jar is located and run Java-jar name. jar to check whether the operation is correct.

Manifest. MF statement.

For example, the package jar is test. jar.

Manifest-version: 1.0
Main-class: Windows. videowindow
Class-path: Lib \ org. Eclipse. swt_3.0.v3346.jar Lib \ org. Eclipse. SWT. win32.win32. x86_3.3.0.v3346.jar

The above is the package description file packaged as jar. the following error always appears.
Cocould not find main class. program will exit
The error message does not actually show the main class. however, if the JAR file fails to be executed, this error occurs. after packaging, run cmd> JAVA-jar your. jar. after the-jar parameter is used, the system classpath variable no longer works. the VM will go to manifest. find related packages under Class-path in MF.

Then you can successfully start from: Windows. the main method of videowindow enters. otherwise, the system will prompt that the main class cannot be found. however, you may not be able to find the north. is there a main class clearly.

If this is not written correctly, an error will always be reported.
Take a look at the above class-path: the following code. separate spaces. There must be no semicolons. Remember.

There must be a line break.This is also worth noting. Otherwise, the system still prompts no mainclass.

Do not delay time because of this small thing. lib/indicates that this jar must be in a lib subdirectory of the directory where the test. jar package is located.

To run jar, run the following command: Java-jar test. jar.
You can also double-click it to open it with javaw.

About Java-classpath. jar; B. jar; Class test. test execution. a. jar; B. jar; must be followed by a semicolon. otherwise, an error is reported. do not expect to leave classpath alone. this is not in eclipse. you must enter the class you want to use. otherwise, you will always be prompted for compilation errors.

Related Article

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.