Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling cmd.exe and an executable from a servlet

Status
Not open for further replies.

bundyld

Programmer
Oct 2, 2001
43
0
0
US
Hello all,

I am having a problem using exec() to call cmd.exe for running an application! For some reason if Tomcat is started as a system service in windows it will not launch the command prompt, However, if I start tomcat through the /bin startup.bat the servlet has no problem launching cmd and the app I want to launch. I am using this command in my servlet! Any suggestions are appr.

Process proc = Runtime.getRuntime().exec("cmd.exe /C C:\\BatchPDF.exe "+rtfFile.getAbsolutePath()+" "+pdfFile.getAbsolutePath());

It is just calling the app BatchPDF to convert a .rtf file to a .pdf file!

Thank you in advance,
Lance
 
Would it be an option not to run the batchpdf.exe without the cmd.exe?
Another way to work around it is with a batchfile. Maybe that would help.

Dennis
 
Yes, forget about the cmd.exe - this is pointless. Runtime.exec() in effect shells out to the OS, and creates a new process to execute the OS native program - it is separate to the process/thread that the JVM is running in, so by executing cmd.exe to execute another exe just means you are kicking off an extra process for no reason ...
 
Ok, I took the cmd.exe out, It looks like the process has started but it is just sitting there. It is not executing the commands that were passed to it! It looks like it was doing the same thing with cmd.exe there was 6 of them running! Any ideas of why it might start the process and not execute the params passed?
 
If the program you execute with Runtime.exec produces output to console, you need to read that output form the RunTime inputstream. Otherwise, the process stuck at inputstream until it has been read.

Here is an example:

Code:
Process proc = Runtime.getRuntime().exec("cmd.exe /C C:\\BatchPDF.exe "+rtfFile.getAbsolutePath()+" "+pdfFile.getAbsolutePath());

BufferedInputStream bis = new BufferedInputStream(proc.getInputStream()); 
BufferedInputStream ebis = new BufferedInputStream(proc.getErrorStream()); 

new ReadBufferThread(bis).start();
new ReadBufferThread(ebis).start(); 

// if you need to wait unitl program completed
proc.waitFor();


// A class to flush out a BufferedInputStream 
private class ReadBufferThread extends Thread {
  BufferedInputStream bis; 
  ReadBufferThread(BufferedInputStream bis) { 
    this.bis = bis; 
  } 

  public void run() {
    String line; 
    while((line = bis.readLine()) != null ) {
      System.out.println(line);
    }
  }
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top