2014-06-11 31 views
-2

我已經創建了我編譯時得到調用一個python腳本的Java調用的Python(類,接口,或枚舉預期)

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

一個java程序錯誤:

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腳本都在同一個目錄下,我該如何解決這個問題?

+0

爲什麼downvotes ??? – Glowie

回答

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"); 
    } 
} 
相關問題