2

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.

13
  • What version of ClearCase are you running? On which OS on the client side? On the server side? Are you running your Java program in a dynamic view? Commented Aug 22, 2016 at 17:44
  • p is null. I think you want assign return type of Runtime.getRuntime().exec(command) to p. Commented Aug 22, 2016 at 17:44
  • @omkarsirra Thank-you for pointing that out. I forgot to add that assignment back in the code but did just now. Either way nothing is returned... I removed the assignment to 'p' because it was not doing anything but went ahead and added it back so somone could possibly help debug it. I got this idiom from here: mkyong.com/java/how-to-execute-shell-command-from-java Commented Aug 22, 2016 at 17:48
  • @VonC I am not sure about the version of ClearCase. I am running Windows on the client side and I do not know the OS ClearCase is running on for the server. Commented Aug 22, 2016 at 17:50
  • @user3870315 cleartool -ver or cleartool -verall should tell you about the version. But are you in a dynamic view? Commented Aug 22, 2016 at 17:51

2 Answers 2

2

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:

Sign up to request clarification or add additional context in comments.

17 Comments

I tried setting the view like so, //try to start a view into ClearCase Process r = Runtime.getRuntime().exec("cleartool startview dynamic_view_name"); but the program just shuts down here and the 'r' variable is null.
I just read more of your post that is hyperlinked from, "As I explained before". I could try using the exec option so that another program is called from there. 0_o
@user3870315 That should not apply to your case, as it is for dynamic view on Unix, not Windows. You need to make sure of your cleartool pwd and cleartool pwv: try those commands through Java and see if you get any result.
Ah, I am glad it does not apply because it was getting very complex in Java. :) I 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. :/ Thank-you for the help. Maybe we can get this to run correctly.
@user3870315 do not use some shortcut. Always test at first in the full dynamic view path: M:\myview\myvob\...
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.