【问题标题】:Location of python source code in Jython/Java integrationpython 源代码在 Jython/Java 集成中的位置
【发布时间】:2015-04-15 05:36:19
【问题描述】:

我可以从this source 集成java/jython。

我下载了jythonbrew,所以jar文件位于/usr/local/Cellar/jython目录下。

我想出了以下脚本来将类文件构建到 _build 目录中,但问题是我必须将 python (jython) 源代码复制到目录中才能使集成工作。

#!/bin/bash

DIRECTORY="_build"
if [ ! -d "$DIRECTORY" ]; then
    # Control will enter here if $DIRECTORY exists.
    mkdir $DIRECTORY
fi

cp Building.py $DIRECTORY
javac -d _build -cp .:/usr/local/Cellar/jython/2.5.3/libexec/jython.jar *.java
java -cp /usr/local/Cellar/jython/2.5.3/libexec/jython.jar:_build org.jython.book.Main

我的问题是如何在我喜欢的任何地方找到 jython 源并教 java 代码查找 jython 文件?

【问题讨论】:

    标签: java python integration jython


    【解决方案1】:

    setupPath() 方法应该首先添加到 JythonObjectFactory 中。

    public static void setupPath(String[] paths)
    {
        PythonInterpreter interpreter = new PythonInterpreter();
    
        interpreter.exec("import sys;");
        for (int i = 0; i < paths.length; i++) {
            interpreter.exec(String.format("sys.path.append(\"%s\")", paths[i]));
        }
    }
    

    然后,在 Main 函数中,应该调用此函数。

    public class Main {
        public static void main(String[] args) {
            JythonObjectFactory factory = JythonObjectFactory.getInstance();
            ArithType a = (ArithType) factory.createObject(ArithType.class, "Arith");
    
            String[] paths = {"DIRECTORY1", "DIRECTORY2"};
            JythonObjectFactory.setupPath(paths);                        
            ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多