有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java eclipse插件可以调用外部python脚本吗?

我的意图是开发一个eclipse插件(我将解释为什么我坚持使用eclipse插件),它将在特定位置执行python脚本(例如C:\MyProject\testscript.py)

我很想从那些已经尝试过类似方法的人那里得到关于这种方法是否有效的回应

为什么使用Eclipse插件
我正在尝试侦听来自服务器的通知。尽管服务器公开普通java api,但侦听机制并不是作为普通java api提供的。但是使用服务器提供的eclipse插件,我可以在eclipse UI中看到通知


共 (2) 个答案

  1. # 2 楼答案

    是的,这是可能的

    示例代码片段

    //Python脚本“test.py”是从eclipse插件执行的

    公共类SampleHandler扩展了AbstractHandler{

    /**
     * The constructor.
     */
    public SampleHandler() {
    }
    
    /**
     * the command has been executed, so extract extract the needed information
     * from the application context.
     */
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
        MessageDialog.openInformation(
                window.getShell(),
                "First",
                "Hello, Eclipse world");
    
        try
        {
            Runtime r = Runtime.getRuntime();
    
            String pythonScriptPath = "D:\\python-samples\\test.py";
            Process p = r.exec("python " + pythonScriptPath);
    
            BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";
            while((line = bfr.readLine()) != null) {
            // display each output line form python script
            System.out.println(line);
        }
        }
        catch (Exception e)
        {
        String cause = e.getMessage();
        if (cause.equals("python: not found"))
            System.out.println("No python interpreter found.");
        }
        return null;
    }
    

    }