如何创建和使用Runnable Jar File.docx

上传人:b****6 文档编号:7593694 上传时间:2023-01-25 格式:DOCX 页数:39 大小:3.02MB
下载 相关 举报
如何创建和使用Runnable Jar File.docx_第1页
第1页 / 共39页
如何创建和使用Runnable Jar File.docx_第2页
第2页 / 共39页
如何创建和使用Runnable Jar File.docx_第3页
第3页 / 共39页
如何创建和使用Runnable Jar File.docx_第4页
第4页 / 共39页
如何创建和使用Runnable Jar File.docx_第5页
第5页 / 共39页
点击查看更多>>
下载资源
资源描述

如何创建和使用Runnable Jar File.docx

《如何创建和使用Runnable Jar File.docx》由会员分享,可在线阅读,更多相关《如何创建和使用Runnable Jar File.docx(39页珍藏版)》请在冰豆网上搜索。

如何创建和使用Runnable Jar File.docx

如何创建和使用RunnableJarFile

RunnableJarPackage

Contents

1,Forprojectwithoutreferencedjarfiles

Method1

Method2(thismethodhelpsyouunderstandthestructureofjarfile)

2,Forprojectwithreferencedjarfiles

Introduction

Mathod1

Method2

Method3

Method4(thismethodhelpsyouunderstandthestructureofjarfile)

Note:

Youwillseemanytext“backtotop”inthisdocument,itonlyhelpsyoujumptocontentspart,doesnothaveanyrealmeaning.

1,Forprojectwithoutreferencedjarfiles:

Method1

Amostsimpleprojectwhichprints“HelloWorld!

”:

publicclassHello{

publicstaticvoidmain(String[]args){

System.out.println("HelloWorld!

");

}

}

Right-clickon“src”,select“Export”,thenselectJava/JARfie

Backtotop

Specifythepathofthenewcreatedjarfile:

Backtotop

NextNext

Backtotop

Finish

Backtotop

Backtotop

Seewhatwehaveinfoldertest:

Seethecontentof“MANIFEST.MF”

Backtotop

Method2(thismethodhelpsyouunderstandthestructureofjarfile)

Step1:

Createatextfile,renameitto“Hello.java”,whichsimplyprints“HelloWorld!

”:

publicclassHello{

publicstaticvoidmain(String[]args){

System.out.println("HelloWorld!

");

}

}

Step2:

Compileitusingjavac.exe,andwegetHello.class,

Delete“Hello.java”,becausewedon'tneeditanymore:

Backtotop

Backtotop

Step3:

Move“Hello.class”tofolder“test”

Step4:

Createanewfoldercalled“META-INF”infolder“test”

Step5:

Createanewtextfilecalled“MANIFEST.MF”infolder“META-INF”

Backtotop

Step6:

Editfile“MANIFEST.MF”,addanewline:

Main-Class:

Hello

Note:

alinebreakisnecessaryattheendofaboveline.

Abovepictureshowsthecorrectinput,

Followingpictureshowsthewronginput:

Difference:

positionofthecursor.

Inanotherword,afterinput“Main-Class:

Hello”inthefile,weneedhit“Enter”keytoinputanewline,whichisnecessary!

Step7:

Run“jarcvfma.jarMETA-INF/MANIFEST.MF.”indirectoryof“test”togetjarfile.

Backtotop

Note:

thedotisnecessary,itmeanscurrentdirectory.

Byabovecommand,wegetfollowingexecutablejarfile:

a.jar

Step8:

Runthejarpackageby“java–jar

Backtotop

Question:

Whatwillhappenifwedon'tinputanewlineinstep6?

Answer:

Wewillgetfollowingerror:

2,Forprojectwithreferencedjarfiles:

Introduction

Thisprojectiscalledtest,thecodeislikebelow:

packagecom.t;

importcom.person.Person;

publicclassHello{

publicstaticvoidmain(String[]args){

Personperson=newPerson();

person.setName(args[0]);

person.setGender(args[1]);

person.setAge(Integer.parseInt(args[2]));

System.out.println("Hello!

\nname:

"+person.getName()+"\ngender:

"+person.getGender()+"\nage:

"+person.getAge());

}

}

 

Backtotop

Itrequiredanotherlibrarycalledperson.jar

 

Backtotop

 

Codeinproject“person”:

packagecom.person;

publicclassPerson{

privateStringname=null;

privateStringgender=null;

privateintage=0;

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicStringgetGender(){

returngender;

}

publicvoidsetGender(Stringgender){

this.gender=gender;

}

publicintgetAge(){

returnage;

}

publicvoidsetAge(intage){

this.age=age;

}

}

Backtotop

 

Weneedmakearunnablejarpackage:

Myeclipseversion:

EclipseJavaEEIDEforWebDevelopers.

Version:

HeliosServiceRelease2

Buildid:

20110218-0911

(c)CopyrightEclipsecontributorsandothers2005,2011.Allrightsreserved.

Visithttp:

//www.eclipse.org/webtools

Mathod1

Step1

Right-clickon“src”Export

Step2

Select“RunnableJARFile”Next

Backtotop

Step3

Specifyexportdestination:

Backtotop

Finish

Select“OK”

ThenwegettheJARfileinthespecifiedpath:

Backtotop

Step4

Run“java-jartest.jartimmale31”indirectory“C:

\Tim”

Result:

succeed

Step5

Extracttest.jartofoldertest,andcheckwhatareinit

Backtotop

Backtotop

Contentof“MANIFEST.MF”:

Manifest-Version:

1.0

Class-Path:

.

Main-Class:

com.t.Hello

Method2

Step1:

thesameasMethod1

Step2:

thesameasMethod1

Step3

Backtotop

Step4

Backtotop

Result:

alsosucceed

Step5

Backtotop

Contentof“MANIFEST.MF”

Manifest-Version:

1.0

Rsrc-Class-Path:

./person.jar

Class-Path:

.

Rsrc-Main-Class:

com.t.Hello

Main-Class:

org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

Method3

Step1:

thesameasMethod1

Step2:

thesameasMethod1

Step3

Backtotop

Backtotop

 

Asyoucansee,inadditionto“test.jar”,anewfolderiscreated:

“test_lib”

Step4

Result:

alsosucceed

Step5

Backtotop

Contentof“MANIFEST.MF”:

Manifest-Version:

1.0

Class-Path:

.test_lib/person.jar

Main-Class:

com.t.Hello

Backtotop

Method4(thismethodhelpsyouunderstandthestructureofjarfile)

Step1:

thesameasMethod1

Step2

Select“JARFile”insteadof“RunnableJARFile”

Backtotop

Backtotop

 

Step3

NextNext

Backtotop

Backtotop

 

Step4

Butwhenwerunit,we`llgetanerror:

“NoClassDefFoundError”

Why?

Afteryouseestep5,youwillunderstand

Step5

Checkthecontentoftest.jar,extractittofolder“test”first

Backtotop

 

Contentof“MANIFEST.MF”:

Manifest-Version:

1.0

Main-Class:

com.t.Hello

Asyoucansee,

Thereisnoperson.jarinthetest.jar

ThereisalsonoClass-Pathdefinitionin“MANIFEST.MF”.

Thisiswhythemainclasscannotfindclass“Person”

Whatisthesolution?

SeeStep6please:

Step6

Exportproject“person”toaJARfile:

Backtotop

Backtotop

 

Backtotop

 

Sonow,wehavetwojarfiles,seeabovepicture.

Createanewfoldercalled“all”,movethetwojarfileintofolder“all”

Right-clickon“test.jar”,extracthere

Note:

compressiontoolsuchas“winrar”isneeded.

Backtotop

Delete“test.jar”

Backupfile“C:

\Tim\all\META-INF\MANIFEST.MF”tootherplace.

Thecontentofwhichis:

Manifest-Version:

1.0

Main-Class:

com.t.Hello

Backtotop

 

Right-clickon“person.jar”,extracthere

Whenyouareaskedifreplaceexistingfiles,select“YestoAll”

Backtotop

 

Delete“person.jar”

Checkcontentof“C:

\Tim\all\META-INF\MANIFEST.MF”again:

Manifest-Version:

1.0

Herewecansee,thekeypart(definitionofMain-Class)isnotinMANIFEST.MFanymore.

Replacefile“C:

\Tim\all\META-INF\MANIFEST.MF”withthebackedupone.

Therefore,wehaveMain-ClassdefinitioninfileMANIFEST.MFagain:

Manifest-Version:

1.0

Main-Class:

com.t.Hello

Backtotop

 

Run“jarcvfmnewtest.jarmeta-inf/manifest.mf.”indirectory“all”:

AndnowwehaveanewJARfilecalled“newtest.jar”:

Backtotop

 

Runthejarfileindirectory“all”:

Succeedagain.

Now,let`sseethefilesstructure:

Backtotop

Conclusion:

bythismethod,wecanmakethisfilestructuremanually.

PutmainclassHello.classandreferencedclassPerson.classanditsparentsfolderinsamemotherfolder

AndkeepcontentofMETA-INF/MANIFEST.MFcorrect,pointingoutthecorrectmainclass.

 

Backtotop

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 调查报告

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1