java - sun.misc.InvalidJarIndexException : Invalid index when importing from com. * Jython 独立包中

标签 java jar classpath jython jython-2.7

当我尝试在应用程序中使用 Jython 独立 JAR 时,出现 InvalidJarIndexException,并且我无法弄清楚我做错了什么。

一旦我尝试使用以“com.”开头的包中的任何 Java 类的 import 语句执行 Python 脚本(例如:“com.foo.Bar”),就会抛出以下异常(被截断):

Traceback (most recent call last):
  File "<string>", line 1, in <module>
sun.misc.InvalidJarIndexException: Invalid index
    at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:1152)
    at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:1062)
    at sun.misc.URLClassPath$JarLoader.findResource(URLClassPath.java:1032)
    at sun.misc.URLClassPath.findResource(URLClassPath.java:225)
    at java.net.URLClassLoader$2.run(URLClassLoader.java:572)
    at java.net.URLClassLoader$2.run(URLClassLoader.java:570)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findResource(URLClassLoader.java:569)
    at java.lang.ClassLoader.getResource(ClassLoader.java:1089)
    at java.net.URLClassLoader.getResourceAsStream(URLClassLoader.java:233)
    at org.python.core.ClasspathPyImporter.tryClassLoader(ClasspathPyImporter.java:221)
    at org.python.core.ClasspathPyImporter.makeEntry(ClasspathPyImporter.java:208)
    at org.python.core.ClasspathPyImporter.makeEntry(ClasspathPyImporter.java:18)
    at org.python.core.util.importer.getModuleInfo(importer.java:174)
    at org.python.core.util.importer.importer_find_module(importer.java:98)
    at org.python.core.ClasspathPyImporter.ClasspathPyImporter_find_module(ClasspathPyImporter.java:134)
    at org.python.core.ClasspathPyImporter$ClasspathPyImporter_find_module_exposer.__call__(Unknown Source)
    at org.python.core.PyBuiltinMethodNarrow.__call__(PyBuiltinMethodNarrow.java:48)
    at org.python.core.imp.find_module(imp.java:761)
    at org.python.core.imp.import_next(imp.java:1158)
    at org.python.core.imp.import_module_level(imp.java:1350)
    at org.python.core.imp.importName(imp.java:1528)
    at org.python.core.ImportFunction.__call__(__builtin__.java:1285)
    at org.python.core.PyObject.__call__(PyObject.java:433)
    at org.python.core.__builtin__.__import__(__builtin__.java:1232)
    at org.python.core.imp.importOneAs(imp.java:1564)
    at org.python.pycode._pyx0.f$0(<string>:1)
    at org.python.pycode._pyx0.call_function(<string>)
    at org.python.core.PyTableCode.call(PyTableCode.java:173)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.Py.runCode(Py.java:1687)
    at org.python.core.Py.exec(Py.java:1731)
    at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:268)
    at com.so.Script.execute(Script.java:20)

这就是我在代码中所做的一切(我实际上是通过 JMenuItem 上的 Swing 操作调用 new Script().execute() 来调用它,这很可能是无关紧要的):

package com.so;

import org.python.core.PyDictionary;
import org.python.core.PySystemState;
import org.python.util.PythonInterpreter;

public class Script {

    public Script() {
    }

    public void execute() {
        PyDictionary table = new PyDictionary();
        PySystemState state = new PySystemState();
        PythonInterpreter interp = new PythonInterpreter(table, state);
        String script;
        script = "" +
"import com.foo.Bar as Bar\n" +
""; 
        interp.exec(script);
    }
}

我的类路径中没有这样的包/类甚至没有关系。但最让我困惑的是,当我认为这必须与类路径相关时,创建了一个具有完全相同的类路径(来自磁盘上相同位置的相同 JAR 文件)的单独模拟项目时,另一个项目在运行时工作得很好并且它执行实际的脚本。

我在这里可能做错了什么?

Java 1.8u241 (x64) 和 jython-standalone-2.7.2.jar 都会发生这种情况以及更早的 2.7.1 版本。堆栈跟踪中的类加载器正在尝试解析“com”。

最佳答案

我完全是偶然发现了罪魁祸首。

我的损坏项目使用了 Glazed Lists 库的旧版本,即 glazedlists-1.8.0_java15.jar。该 JAR 似乎与 jython-standalone-2.7.2.jar 直接不兼容。一旦您将它们放在同一个类路径上并尝试执行 python 脚本(该脚本导入以“com.”开头的任何 Java 包),您最终会遇到 InvalidJarIndexException。更新到该 JAR 的新版本解决了该问题。

因此,如果您在尝试运行 Jython 时遇到类似的异常,我建议您将项目的所有依赖项更新为最新或更新版本。事实上,即使根本不使用 Jython,这也可能是解决方案。

关于java - sun.misc.InvalidJarIndexException : Invalid index when importing from com. * Jython 独立包中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61270546/

相关文章:

java - Netbeans persistence.xml - 持久单元路径的定义

java - 如何从 android-studio 中的自定义 java 类制作 .jar 文件以供其他项目使用

java - mapreduce:我应该避免在映射器/ reducer 中创建对象吗?

java - 方法的final局部变量 'avoid repeat invocations'怎么办?

java - JNA:无法创建结构子类的对象 - 结构字段无效

eclipse - 将外部jar文件添加到Eclipse中的特定库中

java - 无法从 URL 获取 URI,获取 null?

android - 为什么类路径更改后 Android 中的应用程序签名会更改?

java - 使用 CATALINA_OPTS 添加到 Tomcat 类路径

java - Ant JUnit ClassNotFoundException