0

I am trying to start tomcat by startup.bat in java application. I am executing the start command in ProcessBuilder. the problem is startup.bat running only after all threads(including main thread) in java is completed with a Thread.sleep at least 10sec on main thread. another problem is after the startup.bat is started running, System.exit(0) is not working. even stop button in eclipse console is not working. the only way to exit program is manually closing the start up.bat window. How do i gracefully start and stop the tomcat via java. I dont want to embed the tomcat via Tomcat API and jar. tomcat 7 is extracted in my project folder.

code

cJ_
  • 486
  • 1
  • 4
  • 19

2 Answers2

0

Try running these commands in Java ProcessBuilder, make sure execution workdir is set to $tomcat/bin/ folder. This should start tomcat in a separate process.

Start Tomcat cmd.exe /C catalina.bat start

Stop Tomcat cmd.exe /C catalina.bat stop

Your main application could attach a shutdown hook where stop command be run, see this topic. Useful example of a shutdown hook in Java?

Community
  • 1
  • 1
Whome
  • 10,181
  • 6
  • 53
  • 65
  • above is not working. still bat is being lanched after all threads finish or just before program exits. that to if i set thread sleep to 10 sec after process builder start method. – cJ_ Jul 23 '14 at 10:37
0

Use this, It worked for me

    Process child = Runtime.getRuntime().exec("c:\program files\tomcat\bin\startup.bat");
Ravi
  • 13
  • 5