将jar中的python文件捆绑在一起以便通过jython进行访问 [英] Bundling python files inside jar for access via jython

查看:122
本文介绍了将jar中的python文件捆绑在一起以便通过jython进行访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我只需使用jython从java代码执行test.py

In the following code I simply execute test.py from java code using jython

public static void main(String[] args) {
  org.python.util.PythonInterpreter python = new org.python.util.PythonInterpreter();
  python.execfile("test.py");
  ...

我的问题是test.py需要在同一目录下运行jar文件。

我需要在jar中捆绑这个test.py,并且仍然能够使用jython执行它。

My problem is test.py needs to be in same directory from where jar file is run.
I need this test.py bundled inside the jar and still be able to execute it using jython.

如何使用python调用jar文件中的python脚本?使用ClassLoader类中的getResourceAsStream方法读取字符串中的脚本对我来说不适合作为我的测试。 py导入更多python脚本全部捆绑在jar内。

Approach suggested in How do you invoke a python script inside a jar file using python? to read the script in string using getResourceAsStream method from ClassLoader class wont work for me as my test.py imports more python scripts all bundled inside the jar.

刚接触java和jython我真的很困惑。

任何帮助都会受到高度赞赏..!

Being new to java and jython i'm really confused.
Any help will be highly appreciated..!

推荐答案

您可以简单地将test.py放在jar的根目录下,其他脚本的组织方式与普通python中的fs一样。
然后你可以这样做:

You can simply put test.py at the root of the jar, with the other scripts organized as they would be on the fs in normal python. Then you can do something like that:

public static void main(String[] args) {
  org.python.util.PythonInterpreter python = new org.python.util.PythonInterpreter();
  python.exec("import test");
  python.exec("test.callsomthing()");
}

test.py中的导入应该正常工作,包括从中导入其他模块test.py。

the import in test.py should work as normal, including importing other modules from test.py.

这篇关于将jar中的python文件捆绑在一起以便通过jython进行访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆