Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Launching an application from a servlet

  Asked By: Fern    Date: Jun 24    Category: Java    Views: 734
  

I want to execute a batch or unix script which will determine if a
mail server is running, can you tell me how I would do this from a
line or two of java code.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Corey Jones     Answered On: Jun 24

It might make more sense to try to connect with the mail server  directly
with one of the java.net classes and methods.

The following code  should however run your script:

/* RuntimeDemo.java
* @author: Charles Bell
* @version: Nov 12, 2002
* Example use of Runtime.
*/
import java.io.*;
/** Executes the String arguments, displays the commands being
executed, and displays
* any results.
*/
public class RuntimeDemo{
/** Instantiates a class and runs the commands passed in the aguments.
*/
public static void main(String[] args){
RuntimeDemo demo = new RuntimeDemo();
if (args.length == 0){
System.out.println("Useage: java  RuntimeDemo
command");
System.out.println(" or");
System.out.println("Useage: java RuntimeDemo
command arg0 arg1 arg2 ..." );
}else{
demo.runCommand(args);
}
}

/** Runs the commands in a process and displays the results.
*/
public void runCommand(String[] args){
int number = args.length;
try{
String[] commands;
String operatingsystem =
System.getProperty("os.name");
if
(operatingsystem.toLowerCase().indexOf("windows") > 0){
commands = new String[number + 2];
commands[0] = "command.com"; //for Windows
95, 98, ME. etc.
if
(operatingsystem.toLowerCase().indexOf("nt") > 0){
commands[0] = "cmd.exe"; //for
Windows NT
}
commands[1] = "/c";
for (int i = 0; i < number;i++){
commands[i+2] = args[i];
}
}else{
commands = new String[number];
for (int i = 0; i < number;i++){
commands[i] = args[i];
}
}
System.out.print("Executing: ");
for (int i = 0; i< commands.length;i++){
System.out.print(commands[i] + " ");
}
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(commands);
// 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.
CheckStream csin = new
CheckStream(process.getInputStream());
CheckStream cserr = new
CheckStream(process.getErrorStream());
csin.start();
cserr.start();
System.out.print("Waiting for command process to
terminate.");
int done = process.waitFor();
process.destroy();
System.out.println("... Done.");
}catch(InterruptedException ie){
System.out.println("InterruptedException: " +
ie.getMessage());
}catch(IOException ioe){
System.out.println("IOException: " +
ioe.getMessage());
}
}
/** Inner class for checking the results if any of an InputStream.
*/
class CheckStream extends Thread{
BufferedReader br;
String lineread = "";
/** Constructor needs an InputStream to form an anonymous
* InputStreamReader which is used to create a
BufferedReader
* for reading the stream.
*/
CheckStream(InputStream is){
this.br = new BufferedReader(new
InputStreamReader(is));
}
/** Reads the input stream and displays anything returned.
*/
public void run(){
try{
while ((lineread = br.readLine()) != null){
System.out.println(lineread);
}
}catch(IOException ioe){
System.out.println("IOException: " +
ioe.getMessage());
}
}
}
}

 
Didn't find what you were looking for? Find more on Launching an application from a servlet Or get search suggestion and latest updates.




Tagged: