Results 1 to 3 of 3
- 12-11-2008, 12:50 AM #1
Member
- Join Date
- Mar 2008
- Posts
- 8
- Rep Power
- 0
Need to run a java program within a java program
Hi I need to run a java program within a java program. I need to read the output of the program from a java program, my code keeps giving me the error Array index out of bound java 44.
Java Code:public class ExecDemo { static public String[] runCommand(String cmd) throws IOException { // The actual procedure for process execution: // runCommand(String cmd); ArrayList list = new ArrayList(); //storing output. Process proc = Runtime.getRuntime().exec("File 1.java"); //Runtime.getRuntime().exec("C:\User\Peter Dormevil\Desktop\Java programs\File1.java"); InputStream istr = proc.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(istr)); // Create a [BufferedReader] and specify it reads from an input stream. String str; // Temporary String variable while ((str = br.readLine()) != null) list.add(str); // Read to Temp Variable, Check for [null] then Add to (ArrayList)list try { proc.waitFor(); } // Wait for process to terminate and catch any Exceptions. catch (InterruptedException e) { System.err.println("Process was interrupted"); } // Note: proc.exitValue() returns the exit value. ( br.close(); // Done. return (String[])list.toArray(new String[20]); } public static void main(String args[]) throws IOException { try { String outlist[] = runCommand(args[20]); for (int i = 0; i < outlist.length; i++) System.out.println(outlist[i]); // Print the output to screen character by character. // Safe and not very inefficient. } catch (IOException e) { System.err.println(e); } } }
- 12-11-2008, 03:44 AM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 13
You're referring to args[20], which means the 21st argument. I'm guessing you didn't mean that, and aren't passing in 21 arguments to the program.
A possibly more serious problem with this program is that whenever you call a subprocess, you must drain both output stream and error stream. Consider: reading both streams (from separate threads), or using a ProcessBuilder and calling redirectErrorStream() on it to merge the output and error streams.Neil Coffey
Javamex - Java tutorials and performance info
- 12-11-2008, 07:44 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
How you handle 20 command line parameters there?
Similar Threads
-
Java Program for doing FTP
By Rajesh_J2EE in forum New To JavaReplies: 1Last Post: 12-06-2008, 03:35 AM -
need help with java program
By pjr5043 in forum New To JavaReplies: 2Last Post: 09-13-2008, 01:53 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 03:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 10:33 PM -
help with java program
By mattvgt in forum New To JavaReplies: 3Last Post: 07-14-2007, 05:57 PM
Bookmarks