如何创建和使用Runnable Jar FileWord下载.docx

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

如何创建和使用Runnable Jar FileWord下载.docx

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

如何创建和使用Runnable Jar FileWord下载.docx

publicclassHello{

publicstaticvoidmain(String[]args){

System.out.println("

HelloWorld!

"

);

}

}

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

Backtotop

Specifythepathofthenewcreatedjarfile:

NextNext

Finish

Seewhatwehaveinfoldertest:

Seethecontentof“MANIFEST.MF”

Backtotop

Step1:

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

Step2:

Compileitusingjavac.exe,andwegetHello.class,

Delete“Hello.java”,becausewedon'

tneeditanymore:

Step3:

Move“Hello.class”tofolder“test”

Step4:

Createanewfoldercalled“META-INF”infolder“test”

Step5:

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

Step6:

Editfile“MANIFEST.MF”,addanewline:

Main-Class:

Hello

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.

thedotisnecessary,itmeanscurrentdirectory.

Byabovecommand,wegetfollowingexecutablejarfile:

a.jar

Step8:

Runthejarpackageby“java–jar<

jar-file-name>

Question:

Whatwillhappenifwedon'

tinputanewlineinstep6?

Answer:

Wewillgetfollowingerror:

2,Forprojectwithreferencedjarfiles:

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]));

Hello!

\nname:

"

+person.getName()+"

\ngender:

+person.getGender()+"

\nage:

+person.getAge());

 

Backtotop

Itrequiredanotherlibrarycalledperson.jar

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;

Weneedmakearunnablejarpackage:

Myeclipseversion:

EclipseJavaEEIDEforWebDevelopers.

Version:

HeliosServiceRelease2

Buildid:

20110218-0911

(c)CopyrightEclipsecontributorsandothers2005,2011.Allrightsreserved.

Visithttp:

//www.eclipse.org/webtools

Step1

Right-clickon“src”Export

Step2

Select“RunnableJARFile”Next

Step3

Specifyexportdestination:

Select“OK”

ThenwegettheJARfileinthespecifiedpath:

Step4

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

\Tim”

Result:

succeed

Step5

Extracttest.jartofoldertest,andcheckwhatareinit

Contentof“MANIFEST.MF”:

Manifest-Version:

1.0

Class-Path:

.

com.t.Hello

Step1:

thesameasMethod1

alsosucceed

Contentof“MANIFEST.MF”

Rsrc-Class-Path:

./person.jar

Rsrc-Main-Class:

org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

Step2:

Asyoucansee,inadditionto“test.jar”,anewfolderiscreated:

“test_lib”

Result:

alsosucceed

.test_lib/person.jar

Method4(thismethodhelpsyouunderstandthestructureofjarfile)

Select“JARFile”insteadof“RunnableJARFile”

Butwhenwerunit,we`llgetanerror:

“NoClassDefFoundError”

Why?

Afteryouseestep5,youwillunderstand

Checkthecontentoftest.jar,extractittofolder“test”first

Asyoucansee,

Thereisnoperson.jarinthetest.jar

ThereisalsonoClass-Pathdefinitionin“MANIFEST.MF”.

Thisiswhythemainclasscannotfindclass“Person”

Whatisthesolution?

SeeStep6please:

Step6

Exportproject“person”toaJARfile:

Sonow,wehavetwojarfiles,seeabovepicture.

Createanewfoldercalled“all”,movethetwojarfileintofolder“all”

Right-clickon“test.jar”,extracthere

Note:

compressiontoolsuchas“winrar”isneeded.

Delete“test.jar”

Backupfile“C:

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

Thecontentofwhichis:

Right-clickon“person.jar”,extracthere

Whenyouareaskedifreplaceexistingfiles,select“YestoAll”

Delete“person.jar”

Checkcontentof“C:

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

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

Replacefile“C:

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

Therefore,wehaveMain-ClassdefinitioninfileMANIFEST.MFagain:

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

AndnowwehaveanewJARfilecalled“newtest.jar”:

Runthejarfileindirectory“all”:

Succeedagain.

Now,let`sseethefilesstructure:

Conclusion:

bythismethod,wecanmakethisfilestructuremanually.

PutmainclassHello.classandreferencedclassPerson.classanditsparentsfolderinsamemotherfolder

AndkeepcontentofMETA-INF/MANIFEST.MFcorrect,pointingoutthecorrectmainclass.

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

当前位置:首页 > 医药卫生 > 药学

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

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