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