Reputation: 1351
I have this program that I am writing that has this method that is supposed to execute a program but does not do anything. The method in question is as follows:
public void findCC_Data(List<String> l7) {
StringBuffer output = new StringBuffer();
Process p;
try {
for(String sql_file: l7) {
String command = "cleartool describe " + sql_file;
p = Runtime.getRuntime().exec(command);
System.out.println("Executing: " + command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
if (reader.readLine() == null) {
System.out.println("'reader.readLine()' is equal to null");
}
}
} catch(Exception e) {
e.printStackTrace();
}
System.out.println(output.toString());
}
Does anyone know why the command does not do anything and the reader.readLine()
method always returns null?
I am following a tutorial but using the cleartool program instead of the ping program basically. The tutorial is at this URL:https://www.mkyong.com/java/how-to-execute-shell-command-from-java/
Solution
I had the System.out.println(output.toString())
print statement outside of the for loop instead of inside it. Now when I move the SOP statement inside the loop it prints a million plus lines of information on ClearCase version control stuff. To fix put the SOP with the output.toString() inside the loop in the broken code above.
Upvotes: 2
Views: 545
Reputation: 915
Run your command inside a child shell using sh command and redirect the output to nohup, refer nohup and sh command.
once you have the command executed i.e. "nohup cleartool describe " + sql_file; you can get the error or details from nohup.out file and confirm if there is an issue in executing the command.
Upvotes: 0
Reputation: 1324337
One possibility for a program in (java, python, bash, ...) to do nothing with ClearCase command is if said cleartool
command is run within a dynamic view which has been set (cleartool setview
).
As I explained before, the cleartool setview
command opens a subshell in which commands are supposed to be run, which is not the case here (the java program runs in the main shell)
The other possible cause is that you are reading stdout, not stderr, and somehow this commands returns an error (maybe its execution path is not correct).
thought it would not matter anyway because a method I call before the one in question is supposed to change directories to the dynamic view. It appears it does not work as expected though because the result of the
cleartool pwd
command is just my desktop
Yes, each cleartool
command operates in its own shell. You must set the right execution folder for each Java Process run("cleartool ...")
commands, in order for those cleartool commands to start in the right folder.
See "execute file from defined directory with Runtime.getRuntime().exec
", although the answer is a bit dated, and that might have changed with Java8.
As the OP noted, the output.toString()
print statement was outside of the for loop
instead of inside said loop.
You can see additional example in:
Upvotes: 2