Java encapsulates classes and functions into jars, and then uses the jar package in other projects

Source: Internet
Author: User

Originally wanted to install with idea, but after using Maven to find the jar 20,30m is definitely not correct, and later was built with Eclipse, convenient many

Environment:

Eclipse luna,jdk1.8_112

1. Build the jar package, first we write a class, a few common functions

Code:

public class Util {

public void Say () {
System.out.println ("Hello");
}

public int Add (int a,int b) {
return a + B;
}
}

2. We encapsulate these methods into jar packages so that our other projects can be used

Right-click Demo1 Project-"Export->jar file, then next

3. Then finish, you can find the jar package, copy this jar package to eclipse can be used directly, for example, create a new project Demo2

This util is the class we just packed.

Import Com.ontoweb.message.Util;

public class Test {

private static Util Util = new Util ();

public static void Main (string[] arg) {
System.out.println ("This is the main function");
Util.say ();
SYSTEM.OUT.PRINTLN ("The result of the addition is" + Util.add (1, 2));
}

public void Answer () {
Util.say ();
}

public void Add (int a,int b) {
SYSTEM.OUT.PRINTLN ("The result of the addition is" + Util.add (A, b));
}
}

In this class, right-click the run as, Java application, and you can have the results.

4. Import the jar package into the idea in the form of Maven

First change the name of the jar package to what we want, such as Spring-message-1.0.0.jar, then run the MAVEN command (requires MAVEN environment)

MVN Install:install-file-dfile=c:\users\xiabin\desktop\spring-message-1.0.0.jar-dgroupid=com.ontoweb- Dartifactid=spring-message-dversion=1.0.0-dpackaging=jar

-dfile: The location of your jar package

-dgroupid: Is the unique identifier of the project organization (it can be set by itself)

-dartifactid: The unique identifier of the project (which can be determined here)

-dpackaging: Specifies that the package is a jar package

After the installation, we can find our package in the Maven warehouse.

5. Then refer to our Jar package in Maven's Pom.xml

<dependency>
<groupId>com.ontoweb</groupId>
<artifactId>spring-message</artifactId>
<version>1.0.0</version>
</dependency>

Java encapsulates classes and functions into jars, and then uses the jar package in other projects

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.