Java code issue. Please provide some insight (Posted in Software too)

dabuddha

Lifer
Apr 10, 2000
19,579
17
81
Sorry for posting here but I need some quick "advice" on what's going wrong here:

I'm trying to get this program to work. It does various things but the problem that im running into is that I need it to run 2 different unix scripts. Here is one method:

private void extractData(String Id, String extractFileName)
{
Runtime runtime = Runtime.getRuntime();

EnvHelper envHelper = new EnvHelper();

String commandString = dirString + "/run_extract " + Id + " " + extractFileName;
// System.out.println(header + "making system call:" + commandString + ":");
try
{
Process process = runtime.exec(commandString);
}
catch(IOException e)
{
e.printStackTrace();
}

}
This works fine

then a few lines later, I run this method:
private void sendXml(String xmlFileName, String Id)
{

Runtime runtime = Runtime.getRuntime();
String productId = "1";

EnvHelper envHelper = new EnvHelper();

String dirString = envHelper.getProperty("BIN_DIR");
String commandString = dirString + "/run_data_sender " + xmlFileName + " " + Id;

System.out.println("making system call:" + commandString + ":");

try
{
Process process = runtime.exec(commandString);
}
catch(IOException e)
{
e.printStackTrace();
}

return;
}
It prints out the system call and I can run the system call manually. But it never makes the call. Could it be because I created two process objects or two runtime objects?

If you need anymore info, please ask. I modified the code a little to take out some stuff that shouldn't be publicized :)
 

FeathersMcGraw

Diamond Member
Oct 17, 2001
4,041
1
0
Processes created by Runtime.exec likely exist outside the Java machine. It's likely platform VM implementation-dependant what needs to be done to execute/cleanup those processes. If you expect those scripts to be run sequentially, I would look at including a Process.waitFor() on the result of the first exec() call.
 

dabuddha

Lifer
Apr 10, 2000
19,579
17
81
Originally posted by: FeathersMcGraw
Processes created by Runtime.exec likely exist outside the Java machine. It's likely platform VM implementation-dependant what needs to be done to execute/cleanup those processes. If you expect those scripts to be run sequentially, I would look at including a Process.waitFor() on the result of the first exec() call.

So just add a process.waitFor(); after the first process = runtime.exec(blah blah)

and catch the InterruptedException?
 

FeathersMcGraw

Diamond Member
Oct 17, 2001
4,041
1
0
Originally posted by: dabuddha

So just add a process.waitFor(); after the first process = runtime.exec(blah blah)

and catch the InterruptedException?

It's just a guess, but it's all I got.
 

dabuddha

Lifer
Apr 10, 2000
19,579
17
81
Originally posted by: FeathersMcGraw
Originally posted by: dabuddha

So just add a process.waitFor(); after the first process = runtime.exec(blah blah)

and catch the InterruptedException?

It's just a guess, but it's all I got.

Ok Ill try that real quick. Thanks btw for the idea :)
 

dabuddha

Lifer
Apr 10, 2000
19,579
17
81
Originally posted by: FeathersMcGraw
Originally posted by: dabuddha

So just add a process.waitFor(); after the first process = runtime.exec(blah blah)

and catch the InterruptedException?

It's just a guess, but it's all I got.

It worked! ( sorta) I added the waitfor after the 2nd process. I guess what it was doing was it'd execute the command but after exiting the method, the process object would lose scrope and would be destroyed. The 2nd script actually takes some time to complete because it's ftping some large files over.
thanks!