javagirip
asked on
Execute shell script from java
Hi I need to exectue a one line unix (solaris) shell script from my java servlet. I tried using the following code, but got a "broken pipe" IOException. Any idea? Thanks in advance
try
{
Runtime rt = Runtime.getRuntime();
Process child = rt.exec("rsh -l pgg mymachine \"/usr/local/test_ServerCl ient 2 10001 \"");
BufferedWriter outCommand = new BufferedWriter(new OutputStreamWriter(child.g etOutputSt ream()));
outCommand.write("MyShellS cript");
child.waitFor();
int retCode = child.exitValue();
outCommand.flush();
}
catch(InterruptedException ine)
{
System.out.println(ine.get Message()) ;
}
catch(IOException ie)
{
System.out.println(ie.getM essage());
}
try
{
Runtime rt = Runtime.getRuntime();
Process child = rt.exec("rsh -l pgg mymachine \"/usr/local/test_ServerCl
BufferedWriter outCommand = new BufferedWriter(new OutputStreamWriter(child.g
outCommand.write("MyShellS
child.waitFor();
int retCode = child.exitValue();
outCommand.flush();
}
catch(InterruptedException
{
System.out.println(ine.get
}
catch(IOException ie)
{
System.out.println(ie.getM
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oops, I forgot to copy the inner class file, let me try now
If CEHJ's suggestions doesn't work, note that Runtime.exec() runs differently on different systems. On Solaris and AIX, you usually have to use the alternative method form, where you pass in an array:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#exec(java.lang.String[],%20java.lang.String[],% 20java.io. File)
(If that URL doesn't work in your browser, go to http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html and skip down to the version of exec with the signature: exec(String[] cmdarray, String[] envp)
Here's a discussion:
http://forum.java.sun.com/thread.jsp?thread=57009&forum=31&message=144444
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#exec(java.lang.String[],%20java.lang.String[],%
(If that URL doesn't work in your browser, go to http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html and skip down to the version of exec with the signature: exec(String[] cmdarray, String[] envp)
Here's a discussion:
http://forum.java.sun.com/thread.jsp?thread=57009&forum=31&message=144444
ASKER
IT worked.
Thanks a lot!!
Thanks a lot!!
8-)
ASKER
java.lang.NoClassDefFoundE
at the following line of the code
Thread err = new Thread(new OutErrReader(error));