954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

WaitFor Runtime exec to complete (Java)

Hi All,

I wonder if anyone could help?

I have a large Java program which needs at various points to run UNIX commands. The only wayI have found to do this is below (although I would have liked the program to output all terminal commands into the same window (shell) as the program is loaded in) - this code launches new shells silently in the background.

The problem I have is that the code, launches this command and immediately continues to the next lines of code (it doesn't wait for this command to execute and complete before continuing). Due to times varying between folders (due to contents, processing speeds etc) it would be ineffective and inefficient for me to use set wait times.

Does anyone know how to make the program wait for this command to complete before it moves onto the next lines of code?

Thank you very much for your time and help,
Tango2010

Please see code attached:

try           {
	
			System.out.println("Copying files using UNIX file system");
			String[] unixcopy = {"/bin/sh", "-c", "sudo cp -R " + "/filea/ " + "/fileb/"};
			Process punixcopy = Runtime.getRuntime().exec(unixcopy);
			
		}
		
		catch (Throwable t)
		{
			t.printStackTrace();
		}
Tango2010
Light Poster
28 posts since Feb 2010
Reputation Points: 8
Solved Threads: 0
 

Have you tried the waitFor method in the Process class?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
Have you tried the waitFor method in the Process class?

Hi,

Thank you for your reply. Yep I tried that, but for some reason it had 2 effects, in the first instance of implementation;
- It just continued regardless (as if the waitfor wasn't present)
- In the second implementation it just stopped, the process didn't run and the code didn't continue

If my memory serves me correctly I implemented something like;

punixcopy.waitFor();
status=punixcopy.exitValue();
Tango2010
Light Poster
28 posts since Feb 2010
Reputation Points: 8
Solved Threads: 0
 

OK. I'm all Windows here, so I can't check out any Unix situations. All I can do is wish you good luck, so "Good Luck!"

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
OK. I'm all Windows here, so I can't check out any Unix situations. All I can do is wish you good luck, so "Good Luck!"


Thank you anyway, I appreciate your help

Tango2010
Light Poster
28 posts since Feb 2010
Reputation Points: 8
Solved Threads: 0
 
Does anyone know how to make the program wait for this command to complete before it moves onto the next lines of code?


Try removing the first two arguments from your string array i.e.

final String[] args = { "sudo cp", "/source/*", "/target" };

And preferably use ProcessBuilder class for running external processes.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: