无法从java jar运行python脚本

无法从java jar运行python脚本,java,python,javafx,jar,executable-jar,Java,Python,Javafx,Jar,Executable Jar,在IntelliJ中工作时,一切都正常,但在我构建jar之后,一切都停止了。起初,只是我忘了将它放在jar构建配置中,但现在在确保它在那里之后,我仍然无法运行它。以下是我尝试的方法: InputStream script = mainView.class.getResourceAsStream("vizualize3D.py"); Process process = new ProcessBuilder("python3", "-").start() ; Process p1 = Runtim

在IntelliJ中工作时,一切都正常,但在我构建jar之后,一切都停止了。起初,只是我忘了将它放在jar构建配置中,但现在在确保它在那里之后,我仍然无法运行它。以下是我尝试的方法:

InputStream script = mainView.class.getResourceAsStream("vizualize3D.py");
Process process = new ProcessBuilder("python3", "-").start() ;

Process p1 = Runtime.getRuntime().exec("python3 " + script);

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("python3 " + mainView.class.getResourceAsStream("vizualize3D.py"));
尽管资源中有主题,但没有主题工作。我还试图在IntelliJ项目中指定它的路径,但只有在我从jar启动IntelliJ后从IntelliJ运行时,它才起作用

编辑1:
对于那些不了解py文件在jar文件中的人来说,任何涉及您尝试执行
“python3”+script
的选项以及等效选项都不会起作用
script
是一个
InputStream
,不是文件系统上的路径,因此简单地将它与
字符串
连接不会给您带来任何有意义的东西。此外,由于您的脚本不在自己的文件中,而且python解释器也没有简单的方法来提取它,因此像这样简单地调用它是行不通的

但是,您可以做的是执行

python3 -
这里的
-
选项(至少在类似BSD的系统上)意味着“从标准输入读取,并将其解释为脚本”。然后,在Java端,您可以将jar打包的资源作为流读取,并通过管道将其传输到python进程的标准输入

有关为资源选择正确路径的详细信息,请参阅

下面的脚本与类放在同一个包中,对我很有用:

PythonRunner.java:

package example.python;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class PythonRunner {

    public static void main(String[] args) throws Exception {

        String pythonInterpreter = "/usr/bin/python3" ; // default
        if (args.length > 0) {
            pythonInterpreter = args[0] ;
        }

        InputStream script = PythonRunner.class.getResourceAsStream("script.py");
        Process pythonProcess = new ProcessBuilder(pythonInterpreter, "-")
                .start();

        // This thread reads the output from the process and 
        // processes it (in this case just dumps it to standard out)
        new Thread(() ->  {
            try (BufferedReader reader = new BufferedReader(
                    new InputStreamReader(pythonProcess.getInputStream()))) {

                for (String line ; (line = reader.readLine()) != null ;) {
                    System.out.println(line);
                }
            } catch (IOException exc) {
                exc.printStackTrace();
            }
        }).start();

        // read the script from the resource, and pipe it to the
        // python process's standard input (which will be read because
        // of the '-' option)
        OutputStream stdin = pythonProcess.getOutputStream();
        byte[] buffer = new byte[1024];
        for (int read = 0 ; read >= 0 ; read = script.read(buffer)) {
            stdin.write(buffer, 0, read);
        }
        stdin.close();
    }

}
script.py:

导入系统 对于范围(10)内的i: 打印(“垃圾邮件”) 系统出口(0) MANIFEST.MF

清单版本:1.0
主类:example.python.PythonRunner
Eclipse布局:

Jar内容和运行结果:

$jar tf runPython.jar
META-INF/MANIFEST.MF
示例/python/PythonRunner.class
示例/python/script.py
$java-jar runPython.jar
垃圾邮件
垃圾邮件
垃圾邮件
垃圾邮件
垃圾邮件
垃圾邮件
垃圾邮件
垃圾邮件
垃圾邮件
垃圾邮件
$

visuzlize3d.py是否与jar文件位于同一目录中?您是否尝试过使用visualize3D.py的绝对路径?@AkibRhast查看edit1Python不知道如何从jar文件中提取脚本。您必须将其作为单独的文件放在jar文件旁边。@ThomasKläger您可以将其流式传输到python解释器的标准输入。