My intention is to develop an eclipse plugin (I will explain why I am sticking on to eclipse plugin.), that will execute a python script at a specific location (say C:\MyProject\testscript.py).
I am interested in getting responses, from those who have already tried out anything similar, on whether this approach will work.
Why Eclipse Plugin?
I am trying to listen to a notification from a Server. Although the server exposes Plain java APIs, the listening mechanism is not provided as plain java api. But using the eclipse plugins provided by the server, I can see the notifications in the eclipse UI.
2 Answers 2
Yes,it is possible
Sample code snippet
// Python script "test.py" is executed from an eclipse plugin
public class SampleHandler extends 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;
}
}
Comments
I suggest trying to use Jython to bridge from Java (the Eclipse plugin) to your Python. See also Calling Python in Java? (and possibly Invoking python from Java and/or How to execute Python script from Java?)