We help IT Professionals succeed at work.

Runtime.exec from a servlet ?

brian_s
brian_s asked
on
can Runtime.exec be run from a servlet?
Comment
Watch Question

yes, I think so.

Author

Commented:
it works fine when i do:

javac Example.java
java Example

however from a servlet it doesn't work

Author

Commented:
if i cut and paste the command string from my java program and paste it into the dos prompt it works fine-

so something is going wrong from servlet to cmd line execution

please help

Author

Commented:
if i cut and paste the command string from my java program and paste it into the dos prompt it works fine-

so something is going wrong from servlet to cmd line execution

please help

Author

Commented:
if i cut and paste the command string from my java program and paste it into the dos prompt it works fine-

so something is going wrong from servlet to cmd line execution

please help
Mick BarryJava Developer
BRONZE EXPERT
Top Expert 2010

Commented:
Running a command from the command line, is not the same as running it from exec.
The problems probably because of an environement difference.
Ensure you include the full pathname of the executable you are trying to run, and that you supply all the required environment variables.

Or run your command with the command shell.

Author

Commented:
I have it running from the exec if I just invoke that class...

javac whatever.java
java whatever

and it'll work just fine


However, the same exec code through a servlet doesn't work.  No errors are generated!?

I'm using Websphere v3.52 on NT

thanks
Mick BarryJava Developer
BRONZE EXPERT
Top Expert 2010

Commented:
Again the servlet runs in a different environment than what your user login does.
What's the error your getting?

Author

Commented:
I'm not getting any errors, but the process's exitCode() is returning 1 if that helps.


Sorry it took so long for me to get back to this... I tried to get in but Experts was down-

If I watch the task manager on the NT box while I click on the link in my servlet, I first see a CMD.EXE start than a JAVA.EXE

so it seems as if they are both starting, just not properly finishing

any ideas

Author

Commented:
here is some of my code if it helps:



Runtime RunTime = Runtime.getRuntime();
      Process process = null;
      String  dir = "c:\\websphere\\appserver\\hosts\\default_host\\ias\\java_source\\",
              swfName = dir + ProjectId,
              template = dir + "one.swt",
              myExe;
      try{  
         myExe = "cmd /c c:\\jgenerator-1.3.0\\bin\\jgenerate.cmd -param projectName " + "\"" + ProjectName + "\"" +
                                                         " -param sectionOne "+ "\"" + sectionOne + "\"" +  
                                                         " -param sectionTwo "+ "\"" + sectionTwo + "\"" +
                                                         " -param sectionThree "+ "\"" + sectionThree + "\"" +  
                                                         " -param sectionFour "+ "\"" + sectionFour + "\"" +
                                                         " -param sectionFive "+ "\"" + sectionFive + "\"" +
                                                         " -param sectionSix "+ "\"" + sectionSix + "\"" +
                                                         " -param paragraphOne "+ "\"" + paragraphOne + "\"" +
                                                         " -param paragraphTwo "+ "\"" + paragraphTwo + "\"" +
                                                         " -param paragraphThree "+ "\"" + paragraphThree + "\"" +
                                                         " -param paragraphFour "+ "\"" + paragraphFour + "\"" +
                                                         " -param paragraphFive "+ "\"" + paragraphFive + "\"" +
                                                         " -param paragraphSix "+ "\"" + paragraphSix + "\"" +
                                                         " -swf " + swfName + ".swf " + template;
         System.err.println(myExe);
         
         process = RunTime.exec(myExe);
       
         System.err.println("got here also.... process = " + process);
      try {
         int rc = process.waitFor();
       }
       catch (InterruptedException ie) {
        System.err.println(ie);
      }
      System.out.println("process returned " + process.exitValue());
         process.destroy();
      }
      catch (IOException e){  
           System.err.println("_DbFlash CreateSWF failed! IOException...   stacktrace-->");
           e.printStackTrace();
      }



NO exceptions are thrown.....
Java Developer
BRONZE EXPERT
Top Expert 2010
Commented:
Exceptions won't get thrown if there's an error executing the exec(). You just get the error return of the process.

WHat does a return code of 1 from jgenerate.cmd signify?

Probaly worth dumping stdout and stderr for the process also to see if there are any error messages.

Author

Commented:
hello objects.. just logged in to this site for the first time in a while


although i never figured this out (did it through a workaround)... you did help alot so here's the points
Mick BarryJava Developer
BRONZE EXPERT
Top Expert 2010

Commented:
Thanks.