1

I use ProcessBuilder for execute the cmd commands in my application for Start & Stop Derby Network Server. But somethings going wrong and i don't find where the problem. Let me explain it;

Starting Network Server;

//Defining path of db files located
File file= new File(FirstTimeMainFrame.class.getProtectionDomain()
                        .getCodeSource()
                        .getLocation()
                        .getPath().replace(new File(FirstTimeMainFrame.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getName(), "").replace("%20", " "));

String path = file+"\\DB";

//Process Creating
ProcessBuilder builder = new ProcessBuilder();
Process process = null;
String[] command = new String[3];

command[0] = "cmd.exe";
command[1] = "/c"; //This things say to CMD close when commands complete.
command[2] = "cd "+path+" && java -jar derbyrun.jar server start";
builder = new ProcessBuilder(command[0], command[1], command[2]);
builder.redirectErrorStream(true);
process = builder.start();

//Reading CMD outputs
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
  while (true) {
     line = br.readLine();
     if (line == null) { break; }
     System.out.println(line);
  }

When i debug the project i see output for two lines and debug stuck at line = br.readLine(); when While loop come and check for third time. Whole program stuck and can't continue.

Outputs;

Fri Dec 25 20:54:36 EET 2015 : Security manager installed using the Basic server security policy.
Fri Dec 25 20:54:36 EET 2015 : Apache Derby Network Server - 10.12.1.1 - (1704137) started and ready to accept connections on port 1527

Important P.S.: If i remove //Reading CMD outputs codes all things working perfectly. Server Starting, DB Created, Tables Created etc.

Also i tried same CMD Command under Windows directly. When i execute the command, two line writed and Command Prompt window stuck at the flashing cursor (not closed or complete i think) but Derby Server is started without problem in programaticly or directly in Windows.

1
  • Is your question: "how can I tell if the Derby Network Server is up and running?" Dec 25, 2015 at 21:28

1 Answer 1

1

There are actually two processes running in the scenario: CMD process started from java code and Derby server process spawned by CMD.

The output from Derby server process is directed to command line and then can be read in java code. Server process can run infinetly long until it will be terminated, that's why output stream never ends.

Hanging in java code happens because there are no available bytes in the stream at the moment - server process told you that it was successfully initialized and then moved to waiting state.

5
  • I have not problem with start the DB. I know it is started because derby always print these two lines when succesfully started. My problem is in while loop as i said. Because whole program stuck at readLine and can't continue. If i remove the while loop from the soruce code (don't read output of process) whole code succesfully working. I just need identify derby succesfully started and break the while loop. Dec 25, 2015 at 21:08
  • @BlackWhite Your question is 'why it happens'. Method readLine() can terminate if it will see a new line or if stream will be closed. None of this two things will happen in your scenario. That's why your code hangs. Dec 25, 2015 at 21:12
  • Exact problem is how can i achieve this? And why i can't close the releated cmd command after these two lines reading or how can i identify CMD command actually finish the work? Dec 25, 2015 at 21:14
  • @BlackWhite Your derby server process is spawned by CMD process. It will die when CMD will be closed or in case you kill it externally. You can use Process#waitFor() method to wait for process termination, if you're sure that it will terminate. Dec 25, 2015 at 21:22
  • Ah this is the point! CMD is never end because is server alive you said i think. Ok i don't care read the output in server starts. I will check server status with different way after server starting anyway. Please update your answer i will accept it. Thanks for the routed conversation :) Dec 25, 2015 at 21:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.