Use of eclipse hit jar package

Source: Internet
Author: User

Compiling Java code you can of course use Javac directly, but for most of your classmates it's quick to use the Eclipse editor. This article says that I use the eclipse compiled the road of execution, walk many detours, see me one by one way.

Demonstrate the architecture of the system

First look at my directory structure:

This demo code is very simple, the structure is very clear, can not be used in any production, just for demonstration. The general idea is that the Hellojar project provides class library tools for Userjar, and the Hellojar Hello class has only one method:

Package Net.oseye;public class Hello {public string say (string name) {return "Hello," +name;}}

And the Userjar Runmain class is the program entry, call Hellojar function, the code is as follows:

Package Net.oseye;import Net.oseye.hello;public class Runmain {public static void main (string[] args) { System.out.println (New Hello (). Say ("Oseye"));}}

This is the whole architecture, let's go to the class library Hellojar.


use eclipse everywhere jar package

    1. on the Hellojar project, right-click menu , select "Export ...";
    2. In the Export dialog box that pops up, select the Jar file option, such as:

      Why do you choose the jar file instead of the runnable jar file, later on?
    3. Click Next to select the project and export the jar directory

      All the way next or direct finish, complete. If it does not make a mistake, the jar package is successfully exported.

referencing third-party jar packages

Since Userjar uses Hellojar.jar, we now show how to refer to third-party jar packages.

Right-click on the Userjar project to select " Properties , open the dialog box and click on the "Add External JARs" button to select the jar package you want to refer to.

Direct Execution Program

In the DOS command line into the Userjar bin directory execution, reported the following exception:

This is because a third-party jar package is referenced and needs to be set Classpath to perform successfully:

JAVA-CP.; D:\jar\HelloJar.jar Net.oseye.RunMain

It would be cumbersome to refer to many third-party jars. So I want to make Userjar into a jar package to execute.


Execute jar Package

Initially found on the internet can use the Fatjar plug-in, you can include a third-party jar of the project into a package. So the installation of Fatjar, the use is very convenient.

    1. Right-click on the "Build Fat Jar" on the Userjar project;
    2. The dialog box is set as follows:

    3. Next, select the third-party jar package that you want to make into a package, finish.
    4. Execute the JAR package:
      Java-jar Userjar_fat.jar
      Results:
    5. haha, this is not more convenient. In fact, decompression Userjar_fat.jar you can see Hellojar.jar:


use Eclipse to play R unnable JAR File Package

As mentioned above , why choose Jar file instead of runnable jar file, here is the difference between the two. The former only packages the project, excluding the third-party jar, which can be, just like using the Fatjar plugin.

    1. click Next , enter the Export dialog box:

      Be sure to go through the first step of run, otherwise your "Lanuch configuration" is empty. Click Finish to finish.
    2. three options for library handling:
      • Extract required libraries into generated jar: only the CLA required for third-party JAR packages SS extract to be packaged with the project

      • package equired libraries into generated jar: like Fatjar, it's a third-party jar packaged together, just not the same as the Fatjar directory. , Fatjar is placed under the Lib folder, and it is placed in the root directory

      • The third option is to copy the third-party jar package to another subdirectory, and use manifest to set

        where The Manifest content is as follows:
        manifest-version:1.0class-path:. Userjar_lib/hellojar.jarmain-class:net.oseye.runmain 

Summary: The class library is generally packaged into jar file, and executable programs do not need to use the Fatjar plug-in, using eclipse comes with a good.

Use of eclipse hit 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.