【问题标题】:Java calling Python (class, interface, or enum expected)Java 调用 Python(需要类、接口或枚举)
【发布时间】:2014-06-11 14:52:26
【问题描述】:

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

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

编译时出现错误:

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

java程序和python脚本都在同一个目录下,如何解决?

【问题讨论】:

    标签: java python compilation compiler-errors


    【解决方案1】:

    在 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");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      相关资源
      最近更新 更多