The third-party jar is referenced when the java project is compiled into a jar package. How can we solve this problem?

Source: Internet
Author: User

The third-party jar is referenced when the java project is compiled into a jar package. How can we solve this problem?

A lot of Web projects have been done, but we are unfamiliar with pure java projects. Today, during the development of the project, we have a need to put the java project on the linux system and run it, of course, this javaSE project includes the main method. We know that after writing the JavaSE project in IDE, it is very simple to run. as long as the local java environment is installed, right-click the main method ----> run as --> java application, so on linux? At present, we can think of two methods: one is to compress the jar package to the server and run the java-jar ***. jar command to execute the jar package. Another common method is to place java projects on the server and then write a shell script for execution. Method 2 is still being tried. Today, I want to talk about method 1 operations.

It is easy to create a jar package. Just export the jar package in myeclipse or eclipse. The specific method is:

Right-click the project -------> export ----> select JAR file:

------> Next ----->: Specifies the specific storage location of the exported jar package, that is, the name of the jar package:

------> Next ---> default:

------> Select the main method to be executed:

-----> Finesh. This completes the compilation of the JavaSE project into a jar package. Now, you can run java-jar test. jar on the server to execute the specified main method.

So far, if the java project you wrote does not introduce third-party jar packages, there is no problem at all, but once you introduce the framework or the jar package written by your company, the problem is coming quietly. When you execute it, you will find that the NoClassDeFined and other problems are thrown, that is, the class cannot be found. At this time, you may wonder why, this class clearly exists. It's your illusion that you have imported the required jar package in IDE. Of course there will be, however, if the jar file you are executing is put on the server, does it contain the jar file, I thought that the imported jar package was re-packaged when it was packaged into a jar package. Now I think it should not. Well, I have not gone into details yet. Today we will talk about this solution first.

Step 1: Create a lib directory under the same directory where your jar package is stored on your server, and put all the third-party jar packages to be imported. Do not leave one of them blank:

Step 2: Prepare the configuration file MANIFEST in IDE. MF: Create a New MANIFEST under the project directory. for MF files, don't ask why first. Believe me, let's solve our problem first and then say:

The content in the configuration file:

ManifestMain-Version: Version 1.0

Class-Path: Add third-party jar names. Note: Leave a space at the end.

Main-Class: the main method to be executed in the jar package. Remember to write the full path of the Class. Colleague: Leave a space at the end.

OK. The configuration file is ready. continue to the next step.

Step 3: Re-package the jar package. The steps are the same as the original ones: export --- next ---... but the difference is that you need to use your own configuration file as follows:

Step 4: complete the jar package and upload it to your linux server. Remember to put it in the same level directory as lib, so that java-jar test. jar can be executed smoothly.

Today, I just explained one of the solutions. As for the principle, I think you should understand it when writing the list file. It will find the jar package parsing in this class-path. Of course, there are other methods. I have shared this with you. Let's make progress together !!!

Reference from: http://www.cnblogs.com/lanxuezaipiao/p/3291641.html#3306113

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.