NoSuchMethodException:适用于jdk1.8.0中的sun.misc.Launcher $ AppClassLoader.addAppURL(java.net.URL)

时间:2018-08-16 09:00:05

标签: java python java-8

我的代码面临一个非常奇怪的问题。我正在使用下面的代码下载我的jar文件。在JAVA-5和JAVA-6中可以正常工作。

def AddJarToClassloader(self,u):
    #sysloader = java.lang.ClassLoader.getSystemClassLoader()
    sysloader = java.net.ClassLoader.getSystemClassLoader()
    sysclass = sysloader.getClass()
    print 'All Methods:  ' + str(sysclass.getDeclaredMethods())
    method = sysclass.getDeclaredMethod("addAppURL", [java.net.URL])
    print 'All method= ' + str(method)
    method.setAccessible(1)
    jar_a = java.net.URL(u)
    b = method.invoke(sysloader, [jar_a])
    return sysloader

但是,最近,我们将所有Linux机器都更新为Java8。现在,此代码不再起作用。我在日志文件中收到以下错误。谁能帮我解决这个问题?

JAVA-8的错误日志:

[java] All Methods:  array(java.lang.reflect.Method, [public java.lang.Class sun.misc.Launcher$AppClassLoader.loadClass(java.lang.String,boolean) throws java.lang.ClassNotFoundException, private void sun.misc.Launcher$AppClassLoader.appendToClassPathForInstrumentation(java.lang.String), private static java.security.AccessControlContext sun.misc.Launcher$AppClassLoader.getContext(java.io.File[]) throws java.net.MalformedURLException, protected java.security.PermissionCollection sun.misc.Launcher$AppClassLoader.getPermissions(java.security.CodeSource), public static java.lang.ClassLoader sun.misc.Launcher$AppClassLoader.getAppClassLoader(java.lang.ClassLoader) throws java.io.IOException])
 [java] java.lang.NoSuchMethodException: sun.misc.Launcher$AppClassLoader.addAppURL(java.net.URL)
 [java]     at java.lang.Class.getDeclaredMethod(Class.java:2130)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 [java]     at java.lang.reflect.Method.invoke(Method.java:498)
 [java]     at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:175)
 [java]     at org.python.core.PyObject.__call__(PyObject.java:355)
 [java]     at org.python.core.PyMethod.__call__(PyMethod.java:215)
 [java]     at org.python.core.PyMethod.instancemethod___call__(PyMethod.java:221)
 [java]     at org.python.core.PyMethod.__call__(PyMethod.java:206)
 [java]     at org.python.core.PyObject.__call__(PyObject.java:414)
 [java]     at org.python.core.PyObject.__call__(PyObject.java:418)

1 个答案:

答案 0 :(得分:1)

似乎在Java 8中,addAppURL类已从sun.misc.Launcher$AppClassLoader类中删除。但是您可以尝试使用方法java.net.URLClassLoader#addURL,因为URLClassLoadersun.misc.Launcher$AppClassLoader的父级。

所以尝试换行

method = sysclass.getDeclaredMethod("addAppURL", [java.net.URL])

带线

method = sysclass.getSuperclass().getDeclaredMethod("addURL", [java.net.URL])