jython embedded into java

http://mavenhub.com/mvn/central-sync/org.python/jython-standalone/2.5.2

http://wiki.python.org/jython/UserGuide#embedding-jython

Embedding Jython

There are two options for embedding Jython in a Java application. You can make a real Java class out of a Python class, and then call it from your Java code, as previously described, or you can use the PythonInterpreter object

Information on the PythonInterpreter can be found in the JavaDoc documentation for org.python.util.PythonInterpreter.

The following example demonstrates how to use the PythonInterpreter to execute a simple Python program.

The python program:

import sys
print sys
a = 42
print a
x = 2 + 2
print "x:",x

The java code required to execute the python program:

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

public class SimpleEmbedded {

    public static void main(String[] args) throws PyException {
        PythonInterpreter interp = new PythonInterpreter();
        interp.exec("import sys");
        interp.exec("print sys");
        interp.set("a", new PyInteger(42));
        interp.exec("print a");
        interp.exec("x = 2+2");
        PyObject x = interp.get("x");
        System.out.println("x: " + x);
    }
}

Note that the term "PythonInterpreter" does not mean the Python code is interpreted; in all cases, Python programs in Jython are compiled to Java bytecode before execution, even when run from the command line or through the use of methods like exec.

Using JSR 223

JSR 223, Scripting for the Java language, added the javax.script package to Java 6. It allows multiple scripting languages to be used through the same API as long as the language provides a script engine. It can be used to embed Jython in your application alongside many other languages that have script engines such as JRuby or Groovy.

The usage of PythonInterpreter above translates to the following using JSR 223:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class JSR223 {

    public static void main(String[] args) throws ScriptException {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
        engine.eval("import sys");
        engine.eval("print sys");
        engine.put("a", 42);
        engine.eval("print a");
        engine.eval("x = 2 + 2");
        Object x = engine.get("x");
        System.out.println("x: " + x);
    }
}

As of Jython 2.5.1 an implementation of JSR 223 is bundled in jython.jar. Simply add jython to your CLASSPATH and ask for the python script engine.

To customize the path and other variables in sys for a ScriptEngine instance, you need to create a PySystemState and make it active before creating the engine:

System Message: ERROR/3 (<string>, line 702)

Unexpected indentation.
PySystemState engineSys = new PySystemState();
engineSys.path.append(Py.newString("my/lib/directory"));
Py.setSystemState(engineSys);
ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");

After that, any calls engine will use the sys from engineSys. This can be used to make separate system states for individual engines.

         JAVA CLASSPATH 和PATH的区别

3703人阅读 评论(4) 收藏 举报

PATH: 用来指定 java 虚拟机(JVM) 所在在目录,也就是我们最常用的用来编译java源程序的javac.exe 和用来执行 *.class 文件的 java.exe 这两个命令。以我的机器为例吧:我的 jdk 按装目录为 D:/jdk1.5.0_08(确切的说我的这个JDK不是安装版本的,直接解压到 d:/ 下的 ,此时我的 path 就要这样设path=D:/jdk1.5.0_08;D:/jdk1.5.0_08/bin;),设置好了后就可以在命令行中输入 javac , 与 java 命令了,如果这时能打印出命令的 help 文档就说明 path 设置正确了。  当然也可以执行改目录下的rmic命令,去生成对应的stub和skeleton。

CLASSPATH:用来指定我们自己所写的或要用到的类文件 (.jar文件) 所在的目录。 这里有一个小情况:比如我的classpath一开始没有设定,而我的java类文件所在的目录为: d:/javalesson/lesson1  这个文件夹中,此时我把我的命令行窗口所在的目录设置为 d:/javalesson/lesson1(也就是我的类文件所存放的目录) ,这时如果运行某个 .class 文件是一定能找到的。这时我开始设置 classpath 这个目录,比如我设置为classpath=c:/abc这 个目录,这时我的命令行窗口所在的目录没修改仍然为: d:/javalesson/lesson1 (也就是我的类文件所存放的目录,没有改变) , 再运行 java 时就会发生 找不到某某类这样的错误,这是因为你只要设置了 classpath 这个环境变量系统就不会再在当前目录下查询某个类了,所以会发生某某类找不到这样的错误,如果你要既要让系统在 classpath所指定的目录中去找某个类,又要让系统在当前目录下查询某个类,这时你只要让你的 classpath 中多一个 “.” 就可以了,例如:classpath=c:/abc;.这时你的目录即使在 d:/javalesson/lesson1 ,也会找到类文件的。下面以java环境变量为例设置方法: 
1、如果是Win95/98,在/autoexec.bat的最后面添加如下3行语句: 
JAVA_HOME=c:/j2sdk1.4.1 
PATH=%JAVA_HOME%/bin;%PATH% 
CLASSPATH=.;%JAVA_HOME%/lib 
看好了CLASSPATH中第一个".",这个代表当前目录,很多人HelloWorld没有运行起来大多是这个原因。 

2、如果是Win2000或者XP,使用鼠标右击"我的电脑"->属性->高级->环境变量 
系统变量->新建->变量名:JAVA_HOME 变量值:c:/j2sdk1.4.1 
系统变量->新建->变量名:CLASSPATH 变量值:.;%JAVA_HOME%/lib 
系统变量->编辑->变量名:Path 在变量值的最前面加上:%JAVA_HOME%/bin; 
CLASSPATH前面的那个"."和上面的意义是一样的。 

3、如果是Linux用户 
在你的环境中,通常我加在.bashrc文件中,你可以加在你的Profile文件中。 
/usr/local/jdk 为你安装jdk的目录。 
export JAVA_HOME=/usr/local/jdk 
export CLASSPATH=.:$JAVA_HOME/lib 
export PATH=$PATH:$JAVA_HOME/bin


4.写批处理文件时的注意事项
1) “=”号两旁不能有空格,否则会出现很多莫名奇妙的错误
2) 可以用%AAA%来取一个变量 AAA的值
3)注释是##,但是要立马紧跟字符
4)echo,现在还没搞太清楚,下次补上。
5) 在每个命令中都可以加上很多参数,当然要明了这些参数的使用,可以用command --help 来查看

我自己写的一个批处理文件,如下:

##my own bat file,using for generating stub and skeleton

set JAVA_HOME=E:/Java/jdk1.6.0_11
SET PATH=E:/Java/jdk1.6.0_11/bin
SET CLASSPATH=.;%JAVA_HOME%/lib;E:/IBM/rationalsdp7.0/workspace/rmi/bin
rmic -keep -d e:/classes -vcompat -classpath %CLASSPATH% com.ora.rmibook.chapter4.printers.NullPrinter

原文地址:https://www.cnblogs.com/lexus/p/2356464.html