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
- on the Hellojar project, right-click menu , select "Export ...";
- 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?
- 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.
- Right-click on the "Build Fat Jar" on the Userjar project;
- The dialog box is set as follows:
- Next, select the third-party jar package that you want to make into a package, finish.
- Execute the JAR package:
Java-jar Userjar_fat.jar
Results:
- 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.
-
-
- 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.
- 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