Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Executing commands throws an InterruptedException even with waitFor()

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Executing commands throws an InterruptedException even with waitFor()

    Hi forum,

    Please help me. I've been searching for hours for what is wrong and I can safely say im lost

    Here is my code:

    protected Collection<String> executeCommand(String cmd) {
    		//Result of the command output
    		Collection<String> resultString = new ArrayList<String>();
     
    		try
    		{   // Execute command 
    			Runtime rt = Runtime.getRuntime();
    			Process proc = rt.exec(cmd);
     
    			logger.debug("cmd: " + cmd);
     
                           // Class to do outputting...
    			StreamGobbler errorGobbler = new 
    			StreamGobbler(proc.getErrorStream(), "ERROR");            
     
    			// any output?
    			StreamGobbler outputGobbler = new 
    			StreamGobbler(proc.getInputStream(), "OUTPUT");
     
    			// kick them off
    			errorGobbler.start();
    			outputGobbler.start();
     
    			// any error???
    			int exitVal = proc.waitFor();
    			logger.debug("Exit status: " + exitVal);
     
    		} catch (Throwable t)
    		{
    			t.printStackTrace();
    		}
    		return resultString;
    	}
    and...
    class StreamGobbler extends Thread
    	{
    		InputStream is;
    		String type;
     
    		StreamGobbler(InputStream is, String type)
    		{
    			this.is = is;
    			this.type = type;
    		}
     
    		public void run()
    		{
    			try
    			{   logger.debug("in run");
    				InputStreamReader isr = new InputStreamReader(is);
    				BufferedReader br = new BufferedReader(isr);
    				String line=null;
    				while ( (line = br.readLine()) != null)
    					logger.debug(type + ">" + line);    
    			} catch (IOException ioe)
    			{
    				ioe.printStackTrace();  
    			}
    		}
    	}

    This is the error i get:

    java.lang.InterruptedException
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Unknown Source)
    at java.lang.UNIXProcess.waitFor(Unknown Source)
    at gsoa.rim.CommunicationsCheck.executeCommand(Commun icationsCheck.java:177)

    which points to the .waitFor() function call...

    Why would it get interrupted if we are asking it to wait until its done?

    Pleaasee help!


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Executing commands throws an InterruptedException even with waitFor()

    Welcome to the forums LilaH.

    Have you Googled the exception? You ideally need to post something we can compile.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. simple java console program, need help recalling commands
    By zero0000000 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 22nd, 2010, 05:47 AM
  2. ZipInputStream Throws Illegalargument exception for diacritics
    By meghaladevi in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 12th, 2010, 02:32 AM
  3. [SOLVED] File.createTempFile() throws exception on Win7
    By jitinsingla in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 10th, 2010, 02:14 PM
  4. Executing Linux Commands with Java GUI
    By linuxrockers in forum AWT / Java Swing
    Replies: 2
    Last Post: February 15th, 2010, 09:57 PM
  5. Problem with a waitFor() executing a script
    By Baldurian in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 29th, 2010, 09:45 AM