Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within I/O and Streams
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
I/O and Streams
How to call a UNIX script froma java application on AIX (using Korn shell)?
Manishkumar Haria
Greenhorn
Posts: 3
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I think, the subject explains it all
Philip Pross
Ranch Hand
Posts: 76
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
If your class is on the unix server
try
{
Process p = Runtime.getRuntime().exec("/bin/sh " +"di this");
try
{
p.waitFor();
}
catch (InterruptedException e)
{
System.out.println(e.getMessage() );
}
}
catch (IOException e)
{
System.out.println("erreur " + e.getMessage() );
}
Manishkumar Haria
Greenhorn
Posts: 3
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Philip,
Thanks a lot for the tip.
Here's what I tried (almost similar to your tip) and it worked.
import java.io.*; public class ExecScript { public static String Script; public static String Usage="java ExecScript script.name.path [arg0 arg1 ... argn]"; /** This method diplays the contents of the InputStream class passed to it as an argument. */ public static void displayOutput(InputStream is) throws Exception { String s; BufferedReader br = new BufferedReader(new InputStreamReader(is)); while ((s = br.readLine()) != null) { System.out.println(s); } } /** This is the main method. */ public static void main (String args[]) throws Exception { try { String Command = ""; Runtime rtime = Runtime.getRuntime();// Get the current RunTime environment. Script = args[0]; for (int i = 0; i < args.length; i++)// Get the script name and the arguments(if any). { Command = Command + " " + args[i]; } Process shell = rtime.exec(Command);// Submit the script alongwith the arguments(if any). shell.waitFor();// Wait till the script finishes. int RC = shell.exitValue();// Interrogate the exit code of the script. if ( RC == 0 )// Display the normal o/p if script completed successfully. { displayOutput(shell.getInputStream()); } else// Display the normal and the error o/p if script failed. { System.out.println(Script + " exited with RC = " + RC); displayOutput(shell.getErrorStream()); displayOutput(shell.getInputStream()); } } catch(ArrayIndexOutOfBoundsException aie) { System.err.println(""); System.err.println("This program is used to execute shell script."); System.err.println("The usage is : " + Usage); System.err.println("Where"); System.err.println(" script.name.path = The full path of the script to be executed."); System.err.println(" argn = Argument number n. As many as 9 optional arguments can be supplied."); System.err.println(""); System.exit(0); } catch(IOException ioe) { System.err.println("The Virtual machine encountered an IO Exception. Below is the stack trace."); ioe.printStackTrace(); System.exit(1); } catch(Exception e) { System.err.println("The Virtual machine encountered an Exception. Below is the stack trace."); e.printStackTrace(); System.exit(1); } } }
I just have one question about your tip: Does the
string
"di this" identify the script you are trying to execute?
Thanks again for the tip.
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Post your photo!!
Merry Christmas
Hijackers Anonymous
Changing a light bulb the forum style
Show me yours; I'll show you mine...
More...