Question

I am using the Jython classPathHacker idiom that I've seen in several places on the internet. The definitive source is:

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

However, I'm getting the following failure:

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

Here is my code (well mostly SG Langer's code):

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")

The error is occurring in the method = sysclass.getDeclaredMethod("addURL", parameters) line.

Was it helpful?

Solution

Searching for 2nd arg can't be coerced to java.lang.Class[], I came upon the following solution:

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

More detail than can be included here, but google answers your question.

OTHER TIPS

Please use this code instead of yours:

import jarray

class classPathHacker(object):
    """Original Author: SG Langer Jan 2007, conversion from Java to Jython
    Updated version (supports Jython 2.5.2) From http://glasblog.1durch0.de/?p=846

    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

    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"""
        sysloader = self.java.lang.ClassLoader.getSystemClassLoader()
        sysclass = self.java.net.URLClassLoader
        method = sysclass.getDeclaredMethod("addURL", [self.java.net.URL])
        a = method.setAccessible(1)
        jar_a = jarray.array([u], self.java.lang.Object)
        b = method.invoke(sysloader, [u])
        return u

jarLoad = classPathHacker()
a = jarLoad.addFile("..\the\class\path.jar")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top