Subduing CLASSPATH

来源:互联网 发布:淘宝如何拍下商品 编辑:程序博客网 时间:2024/05/01 20:03
原文地址: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.py by Bruce Eckel, 2006 www.MindView.net
Permission granted for free use and distribution as long as this notice is maintained.

Warning! This program modifies the Windows Registry! Use at your own risk.

With no arguments:
    If there are jar files in the current directory, each one is added to the CLASSPATH.
    If there are no jar files, the current directory is added to the CLASSPATH.

With arguments:
    Each argument must be a jar file name. Each argument is added to the CLASSPATH.

Duplicate CLASSPATH entries and nonexistent paths are removed.

I recommend creating C:jars directory, and adding this program to that directory.
Whenever you need to add a new jar, throw it in C:jars and double-click this program.
That way, if you need the same set of jars on another machine, just copy the
directory and run this program.

It's also useful to create a batch/cmd file to run this program, and to place
that file in your Windows PATH. Then you can run the program from any directory.
The batch file might look like this:
python C:jarsSetClassPath.py %1 %2 %3 %4 %5 %6 %7 %8 %9
If you're running Cygwin, you can make a shell file to do the same thing:
python C:/jars/SetClassPath.py $1 $2 $3 $4 $5 $6 $7 $8 $9

This program requires PythonWin, which you can find at:
http://starship.python.net/crew/mhammond/win32/
"""
from _winreg import *
import os, glob, sys
import win32gui, win32con # From PythonWin
path = r'SYSTEMCurrentControlSetControlSession ManagerEnvironment'

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__':
    
# set prevents duplicates, 'if os.path.exists(p)' prunes nonexistent paths:
    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 "Arguments must be jar file names: problem with [" + arg + "]"
                sys.exit(
1)
            
if not os.path.exists(arg):
                
print arg, "does not exist in this directory"
                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选项的话, 我很乐意。
 
原创粉丝点击