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.


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
    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>();
    		{   // 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
    			// any error???
    			int exitVal = proc.waitFor();
    			logger.debug("Exit status: " + exitVal);
    		} catch (Throwable t)
    		return resultString;
    class StreamGobbler extends Thread
    		InputStream is;
    		String type;
    		StreamGobbler(InputStream is, String type)
    			this.is = is;
    			this.type = type;
    		public void run()
    			{   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)

    This is the error i get:

    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
    United Kingdom
    My Mood
    Thanked 293 Times in 226 Posts
    Blog Entries

    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.

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, 10: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, 10:45 AM