This discussion is archived
5 Replies Latest reply: Sep 26, 2013 5:52 AM by gimbal2 RSS

c++ process hangs when started from Java

15a934e2-99a4-4514-9e01-8a0551073b88 Newbie
Currently Being Moderated

I am trying to execute a c++ code from java on a remote Windows machine. In order to deal with the remote part, I have created a Web service from where the actual command is run using Runtime.exec(). The c++ exe is not being called directly from the java code. I have a batch file that eventually calls the exe.

The problem is, both java and c++ processes hang. The java code on server side does handle the output stream and error stream. Also, the c++ code is logging everything in a file on Windows. The strange thing is that, when I remove the WS call and run the java code on server side as a standalone java program, it succeeds. Also, execution of the batch file alone does not hang. Here is the java code:

public class RunCPlusPlusExecutable {
public int runExecutable() {
    int exitValue = 0;
    try {
        Process p = null;
        Runtime rt = Runtime.getRuntime();
        System.out.println("About to execute" + this + rt);
        p = rt.exec("c:/temp/execcplusplus.bat");
        System.out.println("Process HashCode=" + p.hashCode());
        StreamProcessor errorHandler = new StreamProcessor(p.getErrorStream(), "Error");
        StreamProcessor outputHandler = new StreamProcessor(p.getInputStream(), "Output");
        errorHandler.start();
        outputHandler.start();
        exitValue = p.waitFor();
        System.out.println("Exit value : " + exitValue);
        if (exitValue == 0)
            System.out.println("SUCCESS");
        else
            System.out.println("FAILURE");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (Exception e) {
    }
    return exitValue;
}
class StreamProcessor extends Thread {
    private InputStream is = null;
    private String type = null;
    private InputStreamReader isr = null;
    private BufferedReader br = null;
    private FileWriter writer = null;
    private BufferedWriter out = null;
    StreamProcessor(InputStream is, String type) {
        this.is = is;
        this.type = type;
    }
    public void run() {
        try {
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            writer = new FileWriter("*******path to log file********");
            out = new BufferedWriter(writer);
            String line = null;
            while ((line = br.readLine()) != null) {
                Date date = new Date();
                out.write("[" + type + "]: " + date + " : " + line);
                out.newLine();
            }
            writer.flush();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
                if (isr != null)
                    isr.close();
                if (out != null)
                    out.close();
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
String line = null;
            while ((line = br.readLine()) != null) {
                Date date = new Date();
                out.write("[" + type + "]: " + date + " : " + line);
                out.newLine();
            }
            writer.flush();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
                if (isr != null)
                    isr.close();
                if (out != null)
                    out.close();
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

The WS server is running from some admin user. And I have been running the standalone java program from some other user. It seems that the c++ executable is giving referenced memory error when being executed from WS call. There are pop-ups citing the error with OK and Cancel buttons(visible when logged in as admin). The error states


The instruction at 0x05473030 referenced memory at 0x000001d4. The memory could not be read


Any idea what is causing the problem and how to debug it? Why does this memory error comes only when the exe is run through WS call? Please note that I won't be able to debug the c++ code and the web service is an apache axis2 service.

Thanks

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points