Jeremy Fisher
Jeremy Fisher

Reputation: 2782

Empty output file is generated when Java Process is run

I'm having some problems with a certain segment of my code. What is supposed to happen is that the Java program takes some predetermined variables and uses UNIX's "sed" function to replace the Strings "AAA" and "BBB" in a pre-written shell script. I have three methods to do this: one that replaces the Strings in the file using "sed" and writes the output to a different file; one that removes the original file with the "rm" command; and one that renames the output file to the name of the original file using "mv". There are three copies of the shell script in three different directories, and each one should be replaced with it's own specific variables.

The replacement should occur for all three shell script files, but it only occurs for two. On the third shell script, it seems as if the process did not complete, because the byte size of that file is 0. The file that is not replaced is completely random, so it's not the same file not working during each run.

I'm not sure why this error is occuring. Does anyone have any possible solutions? Here is the code:

    public void modifyShellScript(String firstParam, String secondParam, int thirdParam, int fourthParam, String outfileDirectoryPath) throws IOException{
    String thirdDammifParamString = "";
    String fourthDammifParamString = "";
    thirdDammifParamString = Integer.toString(thirdDammifParam);
    fourthDammifParamString = Integer.toString(fourthDammifParam);
    String[] cmdArray3 = {"/bin/tcsh","-c", "sed -e 's/AAA/"+firstDammifParam+"/' -e 's/BBB/"+secondDammifParam+"/' -e 's/C/"+thirdDammifParamString+"/' -e 's/D/"+fourthDammifParam+"/' "+outfileDirectoryPath+"runDammifScript.sh > "+outfileDirectoryPath+"runDammifScript.sh2"};
    Process p;
    p = Runtime.getRuntime().exec(cmdArray3);
}

public void removeOriginalShellScript(String outfileDirectoryPath) throws IOException{
    String[] removeCmdArray = {"/bin/tcsh", "-c", "rm "+outfileDirectoryPath+"runDammifScript.sh"};
    Process p1;
    p1 = Runtime.getRuntime().exec(removeCmdArray);
}

public void reconvertOutputScript(String outfileDirectoryPath) throws IOException{
    String[] reconvertCmdArray = {"/bin/tcsh","-c","mv "+outfileDirectoryPath+"runDammifScript.sh2 "+outfileDirectoryPath+"runDammifScript.sh"};
    Process reconvert; 
    reconvert = Runtime.getRuntime().exec(reconvertCmdArray);
}

Upvotes: 2

Views: 881

Answers (1)

rob
rob

Reputation: 6247

If you haven't already, take a look at When Runtime.exec() won't. One or more Process might be hanging because you aren't consuming the output and error streams. In particular, look at the StreamGobbler in that article's examples.

It could also be the case that you're forgetting to include a trailing slash in outfileDirectoryPath. Read the Process' error stream to see what's going wrong:

InputStream err = p.getErrorStream();
// read the stream and print its contents to the console, or whatever

Keep in mind that you'll want to read the streams in separate threads.

That said, I would personally just do all of this natively in Java instead of relying on external, platform-specific dependencies.

For substring replacement, read the file to a String, then use String.replace and/or String.replaceAll.

You can replace removeOriginalShellScript's body with a call to File.delete:

public void removeOriginalShellScript(String outfileDirectoryPath) throws IOException{
    File f = new File(outfileDirectoryPath, "runDammifScript.sh");
    f.delete();
}

You can replace reconvertOutputScript's body with a call to Files.move:

public void reconvertOutputScript(String outfileDirectoryPath) throws IOException{
    File src = new File(outfileDirectoryPath, "runDammifScript.sh2");
    File dst = new File(outfileDirectoryPath, "runDammifScript.sh");
    Files.move(src, dst);
}

Or just replace both removeOriginalShellScript and reconvertOoutputScript with a call to Files.move, specifying the REPLACE_EXISTING option:

File src = new File(outfileDirectoryPath, "runDammifScript.sh2");
File dst = new File(outfileDirectoryPath, "runDammifScript.sh");
Files.move(src, dst, REPLACE_EXISTING);

Upvotes: 2

Related Questions