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

    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 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);
    
    				}
            }
        }

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

    Default

    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
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Java Program for doing FTP
    By Rajesh_J2EE in forum New To Java
    Replies: 1
    Last Post: 12-06-2008, 03:35 AM
  2. need help with java program
    By pjr5043 in forum New To Java
    Replies: 2
    Last Post: 09-13-2008, 01:53 AM
  3. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 10:33 PM
  5. help with java program
    By mattvgt in forum New To Java
    Replies: 3
    Last Post: 07-14-2007, 05: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
  •