`
happmaoo
  • 浏览: 4606671 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Subduing CLASSPATH

阅读更多
<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_winregimport*
importos,glob,sys
importwin32gui,win32con#FromPythonWin
path=r'SYSTEMCurrentControlSetControlSessionManagerEnvironment'

defgetClassPath():
try:
reg
=ConnectRegistry(None,HKEY_LOCAL_MACHINE)
key
=OpenKey(reg,path,0,KEY_ALL_ACCESS)
i
=0
whileTrue:
try:
name,value,valueType
=EnumValue(key,i)
ifname=='CLASSPATH':
returnvalue
i
+=1
exceptEnvironmentError:
return""
finally:
CloseKey(key)
CloseKey(reg)

defsetClassPath(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([pforpingetClassPath().split(os.pathsep)ifos.path.exists(p)])
pathparts.add(
".")
pathparts.add(
"..")
iflen(sys.argv)>1:
forarginsys.argv[1:]:
ifnotarg.endswith(".jar"):
print"Argumentsmustbejarfilenames:problemwith["+arg+"]"
sys.exit(
1)
ifnotos.path.exists(arg):
printarg,"doesnotexistinthisdirectory"
sys.exit(
1)
pathparts.add(os.path.abspath(arg))
else:
jars
=glob.glob("*.jar")
ifjars:
forjarinjars:
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)
printgetClassPath()


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


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


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics