簡體   English   中英

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

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

我可以從這個源集成java / jython。

我用brew下載了jython ,因此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文件?

應首先將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);                        
        ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM