来谈谈:Windows情况下Java加载DLL
功能实在太强了,在配合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命令。 即便是非英语国家的人发布技术文档,Linux也都首先翻译成英语在国际学术杂志和网络上发表。 掌握在Linux系统中安装软件,在安装Linux工具盘后大致日常所需的软件都会有,一般网络提供下载的软件都会有安装说明。 Linux最大的特点就是其开源性,这一点是十分难得的,这也是它能够存在到现在的原因之一。 在学习linux的工程中,linux学习方法有很多种,这里是小编的学习心得,给大家拿出来分享一下。 随着IT从业人员越来越多,理论上会有更多的人使用Linux,可以肯定,Linux在以后这多时间不会消失。 Linux简单,占内存少,特别是对于程序开发人员来说很方便,如果说windows的成功在于其方便用户的窗口管理界面。 用户下达的命令解释给系统去执行,并将系统传回的信息再次解释给用户,估shell也称为命令解释器,有关命令的学习可参考论坛相关文章,精通英文也是学习Linux的关键。 一定要学好命令,shell是命令语言,命令解释程序及程序设计语言的统称,shell也负责用户和操作系统之间的沟通。
页:
[1]