Results 1 to 3 of 3
  1. #1
    peterdfl is offline Member
    Join Date
    Mar 2008
    Rep Power

    Question 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");
                //Runtime.getRuntime().exec("C:\User\Peter Dormevil\Desktop\Java programs\");
                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
                    String outlist[] = runCommand(args[20]);
                    for (int i = 0; i < outlist.length; i++)
                    // Print the output to screen character by character.
                    // Safe and not very inefficient.
                catch (IOException e)

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Rep Power


    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Colombo, Sri Lanka
    Blog Entries
    Rep Power

Similar Threads

  1. Java Program for doing FTP
    By Rajesh_J2EE in forum New To Java
    Replies: 1
    Last Post: 12-06-2008, 02:35 AM
  2. need help with java program
    By pjr5043 in forum New To Java
    Replies: 2
    Last Post: 09-13-2008, 12:53 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  5. help with java program
    By mattvgt in forum New To Java
    Replies: 3
    Last Post: 07-14-2007, 04:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts