在jython中破解类路径

时间:2014-01-29 21:52:24

标签: classpath jython sikuli

我正在使用我在互联网上的几个地方看过的Jython classPathHacker习语。权威来源是:

http://www.jython.org/jythonbook/en/1.0/appendixB.html#using-the-classpath-steve-langer

然而,我遇到了以下失败:

TypeError ( getDeclaredMethod(): 2nd arg can't be coerced to java.lang.Class[] )

这是我的代码(主要是SG Langer的代码):

class classPathHacker :
##########################################################
# from http://forum.java.sun.com/thread.jspa?threadID=300557
#
# Author: SG Langer Jan 2007 translated the above Java to this
#       Jython class
# Purpose: Allow runtime additions of new Class/jars either from
#       local files or URL
######################################################
    import java.lang.reflect.Method
    import java.io.File
    import java.net.URL
    import java.net.URLClassLoader
    import jarray

    def addFile (self, s):
        #############################################
        # Purpose: If adding a file/jar call this first
        #       with s = path_to_jar
        #############################################

        # make a URL out of 's'
        f = self.java.io.File (s)
        u = f.toURL ()
        a = self.addURL (u)
        return a

    def addURL (self, u):
        ##################################
        # Purpose: Call this with u= URL for
        #       the new Class/jar to be loaded
        #################################

        parameters = self.jarray.array([self.java.net.URL], self.java.lang.Class)
        sysloader =  self.java.lang.ClassLoader.getSystemClassLoader()
        sysclass = self.java.net.URLClassLoader
        print parameters
        method = sysclass.getDeclaredMethod("addURL", parameters)
        a = method.setAccessible(1)
        jar_a = self.jarray.array([u], self.java.lang.Object)
        b = method.invoke(sysloader, jar_a)
        return u

tmp = classPathHacker()
tmp.addFile("C:\Program Files\Sikuli\libs\mysql-connector-java-3.1.14.jar")

错误发生在method = sysclass.getDeclaredMethod("addURL", parameters)行。

1 个答案:

答案 0 :(得分:2)

正在搜索2nd arg can't be coerced to java.lang.Class[],我找到了以下解决方案:

http://python.6.x6.nabble.com/Jython-2-7a2-Issues-with-jarray-and-java-lang-String-Console-prompt-goes-quot-off-quot-td5001336.html

此处可以包含更多详细信息,但Google会回答您的问题。