从java使用Pyrolite运行python脚本

时间:2015-01-19 12:51:13

标签: java python

我的本​​地机器中有一个简单的python脚本,它返回一个字符串。我想从java应用程序运行此脚本并获取返回值。我试图用Pyrolite来做这件事。我下载了jar文件并将它们添加到我的java类路径中。但是我无法运行脚本。

我从readme.txt

获得了以下示例代码
NameServerProxy ns = NameServerProxy.locateNS(null);
PyroProxy remoteobject = new PyroProxy(ns.lookup("Your.Pyro.Object"));
Object result = remoteobject.call("pythonmethod", 42, "hello", new int[]{1,2,3});
String message = (String)result; // cast to the type that 'pythonmethod' returns
System.out.println("result message="+message);
remoteobject.close();
ns.close();

但这不适合我。我的系统配置是

  

操作系统:Windows 8

     

JDK:jdk1.7.0_51

     

Python:2.6

请帮助我。

这就是我编辑代码的方式:

        NameServerProxy ns = NameServerProxy.locateNS(null);
        PyroProxy remoteobject = new PyroProxy();
        Object result = remoteobject.call("C:\\trail1.py", null);
        String message = (String)result; // cast to the type that 'pythonmethod' returns
        System.out.println("result message="+message);
        remoteobject.close();
        ns.close();

1 个答案:

答案 0 :(得分:0)

我并不认为我理解你要做的事情。

如果您仔细阅读本教程,您将发现您无法按照自己的方式使用Pyrolite。它指定您必须将python脚本作为服务器运行,使用名称服务器,您必须在其中定义某些类(例如Your.Pyro.Object)。

然后,您将能够调用您在该python脚本中定义的那些对象,而不是脚本本身。

要做你想做的事,你需要调用像C&#39 {s} fork()这样的函数。然后你就可以打电话给可执行文件了,你也不需要Pyrolite。