views:

331

answers:

2

I am trying to run my c/c++ .exe from eclipse RCP (Java API).

Code:

package com.jkt.rcp.texteditor.handlers;

import java.io.IOException;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;

//import com.jkt.runner.utils.Test;

public class RecordHandler extends AbstractHandler {

    private RecordingThread recordingThread;
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
     System.out.println("inside RecordHandler...");

     recordingThread = new RecordingThread();

     recordingThread.start();

     return null;
    }



}

And code of RecordingThread.java is:

package com.jkt.rcp.texteditor.handlers;

import java.io.IOException;


public class RecordingThread extends Thread {
    private String file = "C:\\workspace\\JProAcceptanceBot\\Record.exe";

    public void run() {
     System.out.println("inside Run()...");

     try {
     Process proc = Runtime.getRuntime().exec(file);


     } catch (IOException e) {
      System.out.println("IOException:"+e);
      e.printStackTrace();
     }

    }
}

Actually RecordHandler.java executes after clicking a eclipse RCP button.
But as soon as I click button, c/c++ exe doesn't respond and my Java program stops responding.
Otherwise if I run this exe inside my eclipse, it runs fine.

This c/c++ exe has been made by using Eclipse CDT and Cygwin.

Please have a look into code and suggest ?

A: 

Be aware of Sun bug 6468220 (also described in bug 6550942 and bug 6511002):

On Windows platform Runtime.exec(String[] cmdarray) does not pass correctly command line arguments, if one of them contains double quotes (").

Passing/Expected           --> Actual

{ "ab\"c", "d\"ef" }       --> { "abc def" }
{ "a b \" c", "d \" e f" } --> { "a b ", "c d", "e f " }
{ "a", "", "b" }           --> { "a", "b" }
{ "\" a" }                 -->     java.lang.IllegalArgumentException

So my question is: what is the exact command line you are trying to execute?

VonC
+1  A: 

I'm not sure, but you might want to immediately start reading the inputstream of proc obtained through proc.GetInputStream(). In the documentation for Process:

All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

This article on javaworld describes the same problem and explains the solution (on page 3).

jilles de wit
I had tried the stuff but no luck.Actually i my native code generates a text file and writes some material to that text file.
Vishal
This issue exists regardless of what the .exe does. Have you looked at the output from proc.GetInputStream() in a debugger?
jilles de wit
Thanx jilles.The problem has been solved with your suggestion.Actually i was doing some mishandling with proc.GetInputStream().Now my c/c++ exe runs fine from RCP.
Vishal
Good to hear it helped! Reason enough accept my answer perhaps? (click the check-mark to the left of my answer, just below the score)
jilles de wit