Java调用Python(期望的类,接口或枚举)

Glo*_*wie -2 python java compiler-errors compilation

我创建了一个调用python脚本的java程序

Runtime r = Runtime.getRuntime();
Process p = r.exec("cmd /c python ps.py sender-ip=10.10.10.10");
Run Code Online (Sandbox Code Playgroud)

当我编译时,我得到错误:

call_py.java:1: error: class, interface, or enum expected
Runtime r = Runtime.getRuntime();
^
call_py.java:2: error: class, interface, or enum expected
Process p = r.exec("cmd /c python ps.py sender-ip=10.10.10.10");
^
2 errors
Run Code Online (Sandbox Code Playgroud)

java程序和python脚本都在同一目录下,我该如何解决这个问题呢?

Anu*_*oob 6

在Python中,您可以只执行代码,但在Java中,它并不那么容易.

您需要将代码放在类中的方法中.

尝试使用以下内容创建名为"PythonCallTest.java"的文件:

public class PythonCallTest {

    public static void main(String[] args) {
        Runtime r = Runtime.getRuntime();
        Process p = r.exec("cmd /c python ps.py sender-ip=10.10.10.10");
    }
}
Run Code Online (Sandbox Code Playgroud)