如何创建和使用Runnable Jar FileWord下载.docx
- 文档编号:20710554
- 上传时间:2023-01-25
- 格式:DOCX
- 页数:39
- 大小:3.02MB
如何创建和使用Runnable Jar FileWord下载.docx
《如何创建和使用Runnable Jar FileWord下载.docx》由会员分享,可在线阅读,更多相关《如何创建和使用Runnable Jar FileWord下载.docx(39页珍藏版)》请在冰豆网上搜索。
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.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 如何创建和使用Runnable Jar File 如何 创建 使用 Runnable
![提示](https://static.bdocx.com/images/bang_tan.gif)