简体   繁体   English

Jython / Java集成中python源代码的位置

[英]Location of python source code in Jython/Java integration

I could integrate the java/jython from this source . 我可以从这个源集成java / jython。

I downloaded the jython with brew , so the jar file is located in /usr/local/Cellar/jython directory. 我用brew下载了jython ,因此jar文件位于/usr/local/Cellar/jython目录中。

I came up with the following script to build the class file into the _build directory, but the issue is that I had to copy the python (jython) source into the directory also to make the integration work. 我想出了以下脚本来将类文件构建到_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

My question is how to locate the jython source anywhere I like and teach java code to find the jython files? 我的问题是如何在我喜欢的任何地方找到jython源并教java代码来查找jython文件?

The setupPath() method should be added into the JythonObjectFactory first. 应首先将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]));
    }
}

Then, in the Main function, this function should be invoked. 然后,在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);                        
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM