5 Replies Latest reply on Sep 26, 2013 12:52 PM by gimbal2

    c++ process hangs when started from Java

    1043163

      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

        • 1. Re: c++ process hangs when started from Java
          jschellSomeoneStoleMyAlias

          One very obvious possibility of course is that a Web Service is probably running as a service. And your other tests, which succeed, are not running as a service.  One possibility from that is that the service and the tests are not running as the same user.

          1 person found this helpful
          • 2. Re: c++ process hangs when started from Java
            1043163

            Yes jschellSomeoneStoleMyAlias, tomcat server where the web service is deployed is running as a Windows NT service... And also the users are not same... How to deal with it... thanks

            • 3. Re: c++ process hangs when started from Java
              gimbal2

              Give the proper rights to the user of the Tomcat server?

              • 4. Re: c++ process hangs when started from Java
                1043163

                Hi gimbal2, the tomcat user does have all the permissions... after the WS call, if I list the tasks running on the server, I do find the c++ executable running there... but it never ends... if it is at all a permission related issue, what sort of permissions does the tomcat user need to have?

                • 5. Re: c++ process hangs when started from Java
                  gimbal2

                  How can I know? Its your environment, not mine. The only thing we know is that when running the stuff through one path things blow up and when you run them through another path, things don't blow up. User rights is an obvious suspect, but not necessarily the actual problem. Generally when rights are the problem, you get some form of "access denied" exception, not a code hang.

                   

                  Another likely possibility is that of network settings; perhaps your server goes through a proxy and your local applications do not, or the other way around.

                   

                  Yet another likely possibility is that the version of Java used is different, or different versions of libraries are no the classpath causing differences in behavior. There is only one way to figure it out: get to know your environment very well and through solid reasoning and experimentation try to figure out where the breaking point is. It all starts with answering this question: what might be different in the environment of the web server, and outside of it? I can't know, only you can. Good luck.