|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
功能实在太强了,在配合exec参数或者通过管道重定向到xargs命令和grep命令,可以完成非常复杂的操作,如果同样的操作用图形界面的工具来完成,恐怕要多花十几陪的时间。
HowtoLoadaJavaNative/DynamicLibrary(DLL)
ThereareseveralwaystomakeitpossiblefortheJavaruntimetofindandloadadynamiclibrary(DLL)atruntime.Iwilllistthembrieflyhere,followedbyexamplesandfurtherexplanationbelow.
CallSystem.loadtoloadtheDLLfromanexplicitlyspecifiedabsolutepath.
CopytheDLLtooneofthepathsalreadylistedinjava.library.path
ModifythePATHenvironmentvariabletoincludethedirectorywheretheDLLislocated.
Specifythejava.library.pathonthecommandlinebyusingthe-Doption.
IfusingEclipse,setthejava.library.pathinEclipsefordevelopment/debugging.
1.CallSystem.loadtoloadtheDLLfromanexplicitlyspecifiedabsolutepath.
Thischoiceremovesalluncertainty,butembedsahard-codedpathwithinyourJavaapplication.Example:
importcom.chilkatsoft.CkZip;publicclassTest{static{try{System.load("C:/chilkatJava/chilkat.dll");}catch(UnsatisfiedLinkErrore){System.err.println("Nativecodelibraryfailedtoload./n"+e);System.exit(1);}}publicstaticvoidmain(Stringargv[]){CkZipzip=newCkZip();System.out.println(zip.version());}}
2.CopytheDLLtooneofthepathsalreadylistedinjava.library.path
ToseethecurrentvalueofthePATHenvironmentvariable,openaMS-DOSpromptandtype:
echo%PATH%
Anotherwayofviewingthejava.library.pathistorunthisJavacode:
Stringproperty=System.getProperty("java.library.path");StringTokenizerparser=newStringTokenizer(property,";");while(parser.hasMoreTokens()){System.err.println(parser.nextToken());}
Note:Thejava.library.pathisinitializedfromthePATHenvironmentvariable.Thedirectoriesmaybelistedinadifferentorder,andthecurrentdirectory"."shouldbepresentinjava.library.path,butmaynotbelistedinthePATHenvironmentvariable.
TheloadLibrarymethodmaybeusedwhenthedirectorycontainingtheDLLisinjava.library.path.Toload"chilkat.dll",callSystem.loadLibrary("chilkat"),asshownhere:
importcom.chilkatsoft.CkZip;publicclassTest{static{try{System.loadLibrary("chilkat");}catch(UnsatisfiedLinkErrore){System.err.println("Nativecodelibraryfailedtoload./n"+e);System.exit(1);}}publicstaticvoidmain(Stringargv[]){CkZipzip=newCkZip();System.out.println(zip.version());}}
3.ModifythePATHenvironmentvariabletoincludethedirectorywheretheDLLislocated.
DothisbymodifyingthePATHenvironmentvariablefromtheWindowsControlPanel.
SetPATHonWindowsXP:
Start->ControlPanel->System->Advanced
ClickonEnvironmentVariables,underSystemVariables,findPATH,andclickonit.
IntheEditwindows,modifyPATHbyaddingthelocationoftheclasstothevalueforPATH.IfyoudonothavetheitemPATH,youmayselecttoaddanewvariableandaddPATHasthenameandthelocationoftheclassasthevalue.
Closethewindow.
ReopenCommandpromptwindow,andrunyourjavacode.
SetPathonWindowsVista:
Rightclick“MyComputer”icon
Choose“Properties”fromcontextmenu
Click“Advanced”tab(“Advancedsystemsettings”linkinVista)
IntheEditwindows,modifyPATHbyaddingthelocationoftheclasstothevalueforPATH.IfyoudonothavetheitemPATH,youmayselecttoaddanewvariableandaddPATHasthenameandthelocationoftheclassasthevalue.
ReopenCommandpromptwindow,andrunyourjavacode.
Important:SettingthePATHenvironmentvariablefromaMS-DOScommandprompthasnoeffectonjava.library.path.Forexample,thisdoesnotwork:
setPATH=c:/chilkatJava;%PATH%javaTest
Also,modifyingthejava.library.pathfromwithinJavacodedoesnotworkeither:
static{try{//Addingadirectorytojava.library.pathherewillnotchangeanything.//System.loadLibrarywillstilllookinthedirectorieslistedinjava.library.path//asitexistedattheverystartoftheprogram.//Theextradirectorypathaddedtojava.library.pathwillnot//besearchedbyloadLibrary.Stringlibpath=System.getProperty("java.library.path");libpath=libpath+";C:/chilkatJava";System.setProperty("java.library.path",libpath);System.loadLibrary("chilkat");}catch(UnsatisfiedLinkErrore){System.err.println("Nativecodelibraryfailedtoload./n"+e);System.exit(1);}}
4.Specifythejava.library.pathonthecommandlinebyusingthe-Doption.
Forexample:
java-Djava.library.path=c:/chilkatJavaTestApp
5.IfusingEclipse,setthejava.library.pathinEclipsefordevelopment/debugging.
OpenProject->Properties,select“JavaBuildPath”,clickonthe“AddExternalJARs…”buttonandaddthe“chilkat.jar”
(stillwithintheProjectPropertiesdialog)Clickonthe“Run/DebugSettings”,selectyourJavaclass,thenclickonthe“Edit…”button.Selectthe“Arguments”tab,thenadd-Djava.library.path=”C:/chilkatJava;${env_var:PATH}”where“C:/chilkatJava”isthedirectorypathcontainingthe“chilkat.dll”file.
</p>
在linux中学习命令的最好办法是学习Shell脚本编程,Shell脚本比起其他语言来学习简单,但是功能却十分强大.通过学习Shell编程,能让你掌握大量的linux命令。 |
|