0

I have one java process starting another java process with JMX support enabled in the following way

java -Dcom.sun.management.jmxremote.port=8088 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -jar app.jar

My problem is if the port 8088 is in use, the jvm of spawned process will throw BindException and the JVM will exit normally. There is no way for me to report back to the process that spawned process did not start because of bind exception. How can I catch/handle the BindException thrown by JVM so that the spawned process can do a System.exit(VALID_ERROR_CODE)? This exception happens even before any control is passed to the user code, so I guess it wouldn't be possible to handle it in the user code.

Any idea as how to handle this use case?

Prasanna
  • 3,703
  • 9
  • 46
  • 74
  • 1
    Does that get logged to stderr (System.err)? If so, you can have the launching process look for that and report the error. – BillRobertson42 Jan 05 '12 at 00:35
  • 1
    If I remember correctly, due to this BindException your jvm shouldn't start in first place, then why do you need to call System.exit(...)? – kosa Jan 05 '12 at 05:44

2 Answers2

2

You should redirect stderr (and maybe also stdout) from your subprocess to the parent process or to System.err (System.out). Look on https://stackoverflow.com/a/1570269/1137529 for more details.

Community
  • 1
  • 1
alexsmail
  • 5,661
  • 7
  • 37
  • 57
0

Is it feasible to check if the port is open before launching the child process? i.e. Try listening on the port and if your socket fails, you know that port is unavailable. The advantage to this solution is that you could generate a new port number and keep testing until you find an open port. Once you have a valid port, you can modify the call to launch the child JVM with the available port.

64BitBob
  • 3,110
  • 1
  • 17
  • 23