【问题标题】:What's the difference between jython-standalone-2.7.0.jar and jython-2.7.0.jarjython-standalone-2.7.0.jar 和 jython-2.7.0.jar 有什么区别
【发布时间】:2015-07-15 16:33:27
【问题描述】:

我写了一个Java例子,代码是:

import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.List;

class JythonExample {

  public static void main(String args[]) throws ScriptException {
    listEngines();

    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine pyEngine = mgr.getEngineByName("python");

    try {
      pyEngine.eval("print \"Python - Hello, world!\"");
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    final PythonInterpreter interpreter = new PythonInterpreter();
    interpreter.exec("print \"Python - Hello, world!\"");

    PyObject result = interpreter.eval("2 + 3");
    System.out.println(result.toString());
  }

  public static void listEngines(){
    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> factories =
        mgr.getEngineFactories();
    for (ScriptEngineFactory factory: factories) {
      System.out.println("ScriptEngineFactory Info");
      String engName = factory.getEngineName();
      String engVersion = factory.getEngineVersion();
      String langName = factory.getLanguageName();
      String langVersion = factory.getLanguageVersion();
      System.out.printf("\tScript Engine: %s (%s)\n",
          engName, engVersion);
      List<String> engNames = factory.getNames();
      for(String name: engNames) {
        System.out.printf("\tEngine Alias: %s\n", name);
      }
      System.out.printf("\tLanguage: %s (%s)\n",
          langName, langVersion);
    }
  }
}

在我的pom.xml 中,如果我使用:

<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython-standalone</artifactId>
    <version>2.7.0</version>
</dependency>

然后我可以成功运行java -jar target/jython-example-1.0-SNAPSHOT.jar,顺便说一下,我使用maven-assembly-plugin构建了一个可运行的jar。

如果我使用:

<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython</artifactId>
    <version>2.7.0</version>
</dependency>

然后当我运行java -jar target/jython-example-1.0-SNAPSHOT.jar 时,我总是会收到以下错误:

ScriptEngineFactory Info
    Script Engine: jython (2.7.0)
    Engine Alias: python
    Engine Alias: jython
    Language: python (2.7)
ScriptEngineFactory Info
    Script Engine: Oracle Nashorn (1.8.0_31)
    Engine Alias: nashorn
    Engine Alias: Nashorn
    Engine Alias: js
    Engine Alias: JS
    Engine Alias: JavaScript
    Engine Alias: javascript
    Engine Alias: ECMAScript
    Engine Alias: ecmascript
    Language: ECMAScript (ECMA - 262 Edition 5.1)
java.lang.NullPointerException
    at me.soulmachine.JythonExample.main(JythonExample.java:21)
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
  * sys.path: ['/home/programmer/src/github/JythonExample/JythonExample/target/Lib', '__classpath__', '__pyclasspath__/']
    This attribute might be including the wrong directories, such as from CPython
  * sys.prefix: /home/programmer/src/github/JythonExample/JythonExample/target
    This attribute is set by the system property python.home, although it can
    be often automatically determined by the location of the Jython jar file

You can use the -S option or python.import.site=false to not import the site module

看来pyEnginenull

所以我想知道jython-standalone-2.7.0.jarjython-2.7.0.jar有什么区别

【问题讨论】:

  • 不同之处在于独立的 jar 不使用缓存,因此(理论上)可以部署给最终用户。这与您的代码无法导入站点模块一致。 -- 题外话 - 你能提供更多关于构建可运行 jar 的信息吗?我在 2.7 中遇到了问题

标签: jython jython-2.7


【解决方案1】:

我认为导致您的问题的主要区别在于 jython-standalone jar 提供 Lib/(其中包含 site.py),而 jython jar 不提供。

https://github.com/scijava/jython-shaded 对该问题以及其他问题进行了更深入的描述,并提供了一个替代 jar 来解决描述中提到的一些问题。

我没有使用 scijava:jython-shaded 的经验,但我将其替换为您的 pom(对于我的设置,我还必须将 jdk.version 更改为 1.7 和 JythonExample)并且您的示例运行。

【讨论】:

    【解决方案2】:

    我刚刚发现的同样错误的一个问题是 maven build 2.7.0 不包含 lib 文件夹。这可能是发布版本的构建错误。我不得不向上移动 b2 构建,它在提供的 jar 中正确包含 lib 文件夹。

    问题maven 2.7.0 jar:
    <dependency> <groupId>org.python</groupId> <artifactId>jython-standalone</artifactId> <version>2.7.0</version> </dependency>

    包含 lib 文件夹的工作 maven 2.7.1b2:
    <dependency> <groupId>org.python</groupId> <artifactId>jython-standalone</artifactId> <version>2.7.1b2</version> </dependency>

    注意:如果您直接从 Jython 站点下载该 jar,它确实正确包含 lib 文件夹。这只是 maven 存储库版本。

    【讨论】:

      猜你喜欢
      • 2016-05-05
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 2015-03-26
      相关资源
      最近更新 更多