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

Have you tried the waitFor method in the Process class?

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

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!"

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.