1、如何创建和使用Runnable Jar FileRunnable Jar PackageContents1, For project without referenced jar filesMethod1Method 2(this method helps you understand the structure of jar file)2, For project with referenced jar filesIntroductionMathod 1Method 2Method 3Method 4 (this method helps you understand the structu
2、re of jar file)Note: You will see many text “back to top” in this document, it only helps you jump to contents part, does not have any real meaning.1, For project without referenced jar files:Method 1A most simple project which prints “Hello World!”:public class Hello public static void main(String
3、args) System.out.println(Hello World!); Right-click on “src”, select“Export”, then select Java/JAR fieBack to topSpecify the path of the new created jar file:Back to topNextNextBack to topFinishBack to topBack to topSee what we have in folder test:See the content of “MANIFEST.MF”Backto topMethod 2(t
4、his method helps you understand the structure of jar file)Step 1:Create a text file, rename it to“Hello.java”, which simply prints “Hello World!”:public class Hello public static void main(String args) System.out.println(Hello World!); Step 2:Compile it using javac.exe, and we get Hello.class,Delete
5、 “Hello.java”, because wedont need it any more:Back to topBack to topStep 3:Move “Hello.class” to folder “test”Step 4:Create a new folder called “META-INF” in folder “test”Step 5:Create a new text file called “MANIFEST.MF” in folder “META-INF”Back to topStep 6:Edit file “MANIFEST.MF”, add a new line
6、:Main-Class: HelloNote: a line break is necessary at the end of above line.Above picture shows the correct input, Following picture shows the wrong input:Difference:position of the cursor.In another word, after input “Main-Class: Hello” in the file, we need hit “Enter” key to input a new line, which
7、 is necessary!Step 7:Run “jar cvfm a.jar META-INF/MANIFEST.MF .” in directory of “test” to get jar file.Back to topNote: the dot is necessary, it means current directory.By above command, we get followingexecutable jar file: a.jarStep 8:Run the jar package by “java jar ”Back to topQuestion:What will
8、 happen if we dont input a new line in step 6?Answer:We will get following error:2, For project with referenced jar files:IntroductionThis project is called test, the code is like below:package com.t;importcom.person.Person;publicclass Hello publicstaticvoid main(String args) Person person=newPerson
9、(); person.setName(args0); person.setGender(args1); person.setAge(Integer.parseInt(args2); System.out.println(Hello!nname: +person.getName()+ngender: +person.getGender()+nage: +person.getAge(); Back totopIt required another library called person.jarBack to topCode in project “person”:packagecom.pers
10、on;publicclass Person private String name=null; private String gender=null; privateintage=0; public String getName() returnname; publicvoidsetName(String name) this.name = name; public String getGender() returngender; publicvoidsetGender(String gender) this.gender = gender; publicintgetAge() returna
11、ge; publicvoidsetAge(int age) this.age = age; Back to topWe need make a runnable jar package:My eclipse version:Eclipse Java EE IDE for Web Developers.Version: Helios Service Release 2Build id: 20110218-0911(c) Copyright Eclipse contributors and others 2005, 2011. All rights reserved.Visit http:/www
12、.eclipse.org/webtoolsMathod 1Step 1Right-click on “src” ExportStep 2Select “Runnable JAR File” NextBack to topStep 3Specify export destination:Back to topFinishSelect “OK”Then we get the JAR file in the specified path:Back to topStep 4Run “java -jar test.jar tim male 31” in directory “C:Tim”Result :
13、 succeed Step 5Extract test.jar to folder test, and check what are in itBack to topBack to topContent of “MANIFEST.MF”:Manifest-Version: 1.0Class-Path: .Main-Class: com.t.HelloMethod 2Step 1 : the same as Method 1Step 2: the same as Method 1Step 3Back to topStep 4Back to topResult : also succeed Ste
14、p 5Back to topContent of “MANIFEST.MF”Manifest-Version: 1.0Rsrc-Class-Path: ./ person.jarClass-Path: .Rsrc-Main-Class: com.t.HelloMain-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoaderMethod 3Step 1 : the same as Method 1Step 2 : the same as Method 1Step 3Back to topBack to topAs you can
15、see, in addition to “test.jar”, a new folder is created: “test_lib”Step 4Result: also succeedStep 5Back to topContent of “MANIFEST.MF”:Manifest-Version: 1.0Class-Path: .test_lib/person.jarMain-Class: com.t.HelloBack to topMethod 4(this method helps you understand the structure of jar file)Step 1: th
16、e same as Method 1Step 2Select “JAR File” instead of “Runnable JAR File”Back to topBack to topStep 3NextNextBack to topBack to topStep 4But when we run it, well get an error: “NoClassDefFoundError”Why? After you see step 5, you will understandStep 5Check the content of test.jar , extract it to folde
17、r “test” firstBack to topContent of “MANIFEST.MF”:Manifest-Version: 1.0Main-Class: com.t.HelloAs you can see, There is no person.jar in the test.jarThere is also no Class-Pathdefinition in “MANIFEST.MF”.This is why the main class cannot find class “Person”What is the solution? See Step 6 please:Step
18、 6Export project “person” to a JAR file:Back to topBack to topBack to topSo now, we have two jar files, see above picture.Create a new folder called “all”, move the two jar file into folder “all”Right-click on “test.jar”, extract here Note :compression tool such as “winrar” is needed.Back to topDele
19、te “test.jar”Back up file “C:TimallMETA-INFMANIFEST.MF” to other place.The content of which is:Manifest-Version: 1.0Main-Class: com.t.HelloBack to topRight-click on “person.jar”, extract here When you are asked if replace existing files, select “Yes to All”Back to topDelete “person.jar”Check content
20、 of “C:TimallMETA-INFMANIFEST.MF” again:Manifest-Version: 1.0Here we can see, the key part (definition of Main-Class) is not in MANIFEST.MF any more.Replace file “C:TimallMETA-INFMANIFEST.MF” with the backed up one.Therefore, we have Main-Class definition in file MANIFEST.MF again:Manifest-Version:
21、1.0Main-Class: com.t.HelloBack to topRun “jar cvfm newtest.jar meta-inf/manifest.mf .” in directory “all”:And now we have a new JAR file called “newtest.jar”:Back to topRun the jar file in directory “all”:Succeed again.Now, lets see the files structure:Back to topConclusion: by this method, we can make this file structure manually.Put main class Hello.class and referenced class Person.class and its parents folder in same mother folderAnd keep content of META-INF/MANIFEST.MF correct, pointing out the correct main class.Back to top
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1