原文地址:http://www.artima.com/weblogs/viewpost.jsp?thread=172953第一,将文件放到jre\lib\ext" />

Subduing CLASSPATH

系统 1085 0
<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/46860.html" frameborder="0" width="468" scrolling="no" height="60"></iframe>
原文地址:http://www.artima.com/weblogs/viewpost.jsp?thread=172953

第一, 将文件放到jre\lib\ext目录中有什么不好呢?最近我听说可能有某种安全问题, 所以禁止那样做。可能在某些情况下的确是那样子。我可不想知道所有的那些情况。

此外, 在配置CLASSPATH的时候, 你不得不点击一堆目录去找到jre\lib\ext, 并且至少在我的系统上看起来有两个地方存在这个目录, 一个是JDK, 另一个是JRE。一半儿的时候我都把文件放错的位置。太令人恼火了。

如果将所有的jar文件都放到一个文件夹下, 比如说是c:\jar, 会怎么样呢?我不认为那有什么不好, 而且看起来那样做更易于管理。而且如果你想要将你设置从台式机移到你的笔记本上, 你所要做的工作就是拷贝那个文件夹就够了。

另外的问题就是每次你要向CLASSPATH中添加一个新的jar文件或者新的目的话, 你不得不去在一个很小的窗口下修改环境变量, 如果不可以那么做的话, 我会很高兴的。而且在你的CLASSPATH中很容易存在一些错误的, 或者不存在的文件或目录, 我也希望那些错误可以被自动的修正。

在这里我向你们推荐Python这个工具。对于处理文件或者目录的工作来说, 使用Python是非常便利的。更令人兴奋的是, 在Python中有一个_winreg模块, 它是Python分发版本中的一个标志模块, 可以帮助你直接去修改注册表。

下面的程序可以从你的注册表中提取CLASSPATH的设置,所以即使CLASSPATH被一个命令窗口临时修改了也是没有关系的,然后踢出掉无效和重复的地址, 然后再做下面三个之中的一个工作:
如果当前目录下面有一些jar文件,而你又没有提供一些命令行参数, 所有的这些jar文件就被添加到你的CLASSPATH中去了。
如果在当前目录下面没有jar文件, 这个文件夹就被添加到你的CLASSPATH中去了。
如果你提供了一些命令行参数, 那些参数就被当作是jar文件而添加到你的CLASSPATH中去了。

所有如果你在你的c:\jar目录中执行改程序的话, 每次你想要添加一个新的jar到你的CLASSPATH中去, 你所要做的工作就是双击这个程序就够了。

最后, 我想说的是这对于初学者应该是一个比较理想的解决方案, 不必将大量的时间浪费在处理CLASSPATH这个问题上。

下面的程序google帮了我不少忙, 而且 这里 有一片文章也是很有用的。
# !python
"""
SetClasspath.pybyBruceEckel,2006www.MindView.net
Permissiongrantedforfreeuseanddistributionaslongasthisnoticeismaintained.

Warning!ThisprogrammodifiestheWindowsRegistry!Useatyourownrisk.

Withnoarguments:
Iftherearejarfilesinthecurrentdirectory,eachoneisaddedtotheCLASSPATH.
Iftherearenojarfiles,thecurrentdirectoryisaddedtotheCLASSPATH.

Witharguments:
Eachargumentmustbeajarfilename.EachargumentisaddedtotheCLASSPATH.

DuplicateCLASSPATHentriesandnonexistentpathsareremoved.

IrecommendcreatingC:jarsdirectory,andaddingthisprogramtothatdirectory.
Wheneveryouneedtoaddanewjar,throwitinC:jarsanddouble-clickthisprogram.
Thatway,ifyouneedthesamesetofjarsonanothermachine,justcopythe
directoryandrunthisprogram.

It'salsousefultocreateabatch/cmdfiletorunthisprogram,andtoplace
thatfileinyourWindowsPATH.Thenyoucanruntheprogramfromanydirectory.
Thebatchfilemightlooklikethis:
pythonC:jarsSetClassPath.py%1%2%3%4%5%6%7%8%9
Ifyou'rerunningCygwin,youcanmakeashellfiletodothesamething:
pythonC:/jars/SetClassPath.py$1$2$3$4$5$6$7$8$9

ThisprogramrequiresPythonWin,whichyoucanfindat:
http://starship.python.net/crew/mhammond/win32/
"""
from _winreg import *
import os,glob,sys
import win32gui,win32con # FromPythonWin
path = r ' SYSTEMCurrentControlSetControlSessionManagerEnvironment '

def getClassPath():
try :
reg
= ConnectRegistry(None,HKEY_LOCAL_MACHINE)
key
= OpenKey(reg,path,0,KEY_ALL_ACCESS)
i
= 0
while True:
try :
name,value,valueType
= EnumValue(key,i)
if name == ' CLASSPATH ' :
return value
i
+= 1
except EnvironmentError:
return ""
finally :
CloseKey(key)
CloseKey(reg)

def setClassPath(newPath):
try :
reg
= ConnectRegistry(None,HKEY_LOCAL_MACHINE)
key
= OpenKey(reg,path,0,KEY_ALL_ACCESS)
SetValueEx(key,
' CLASSPATH ' ,0,REG_SZ,newPath)
win32gui.SendMessage(win32con.HWND_BROADCAST,win32con.WM_SETTINGCHANGE,0,
' Environment ' )
finally :
CloseKey(key)
CloseKey(reg)

if __name__ == ' __main__ ' :
# setpreventsduplicates,'ifos.path.exists(p)'prunesnonexistentpaths:
pathparts = set([p for p in getClassPath().split(os.pathsep) if os.path.exists(p)])
pathparts.add(
" . " )
pathparts.add(
" .. " )
if len(sys.argv) > 1 :
for arg in sys.argv[ 1 :]:
if not arg.endswith( " .jar " ):
print " Argumentsmustbejarfilenames:problemwith[ " + arg + " ] "
sys.exit(
1 )
if not os.path.exists(arg):
print arg, " doesnotexistinthisdirectory "
sys.exit(
1 )
pathparts.add(os.path.abspath(arg))
else :
jars
= glob.glob( " *.jar " )
if jars:
for jar in jars:
pathparts.add(os.path.abspath(jar))
else :
pathparts.add(os.getcwd())
result
= list(pathparts)
result.sort()
newClasspath
= os.pathsep.join(result) + os.pathsep
setClassPath(newClasspath)
print getClassPath()


如果你们当中有一些linux爱好者的话, 想要在上面的程序中添加一个linux选项的话, 我很乐意。


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1156132


Subduing CLASSPATH


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论