To hit the Java program and run the JAR package

Source: Internet
Author: User

1) Then the previous blog continues to say that after the manual compilation, the code into a jar package, and then directly "Java-jar Lz.jar" run the unsuccessful problem. Or the code first:

This is the demo class:

 Package org.lz.demo.a;  Public class demo{    public  String getcontent () {        return "oh,yeah,done!"  ;    }};

Next, this is the Importdemo class that called the demo class, and the two classes are not in a package:

 package org.lz.demo.b;    // Define a package Import Org.lz.demo.a.demo;  Public class importdemo{    publicstaticvoid  main (String args[]) {        System.out.println (new  Demo (). getcontent ());}    ;

Following the manual compilation of the previous blog post (http://www.cnblogs.com/lz3018/p/5227502.html), the results are as follows:

JAR-CVF Lz.jar org

Generate the Lz.jar package.

Java-jar Lz.jar

Tips are as follows:

This is because of the meta-inf\manifest in the jar package. The Main-class attribute is not added in MF (main-class: followed by a space and then entered (Org.lz.demo.b.importdemo

), that is, the main class, after adding this file content is:

manifest-version:1.0Created-by:1.8. 0_60 (Oracle Corporation) Main-class: Org.lz.demo.b.importdemo

Then use this manifest file to regenerate the jar package, copy the modified MANIFEST.MF, and the org in the same directory, and then regenerate the jar package using the following command:

Jar CFM Lz.jar MANIFEST. MF org ( jar cfm [jar file name] [additional manifest file name] [import file])

Then, execute:

Java-jar Lz.jar;

The program runs successfully. As follows:

Reference: Http://zh.wikihow.com/%E8%BF%90%E8%A1%8C.jar%E5%BD%A2%E5%BC%8F%E7%9A%84Java%E7%A8%8B%E5%BA%8F

2) However, if the demo class and the Importdemo class both refer to the third-party jar package, then follow this method will be prompted to find the referenced third-party library, which is the reason why the ClassLoader did not load successfully, the following specific tips:

The workaround is also to modify the contents of the MANIFEST.MF manifest file, adding the "Class-path" attribute. Add the following content:

manifest-version:1.0Created-by:1.8. 0_60 (Oracle Corporation) Main-Class: Lz.src.gauss.gaussClass

Well, the relative path of the two libraries referenced as the "Class-path" value (: The number follows a space) if there are multiple reference libraries, separate them with spaces. Then use this manifest file to regenerate the jar package

Jar CFM Lzsrc.jar MANIFEST. MF LZ

then Java -jar Lzsrc.jar

The program runs successfully, as follows:

The above is the third method in the reference (http://www.cnblogs.com/adolfmc/archive/2012/10/07/2713562.html).

To hit the Java program and run the JAR package

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.