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.
Asked
Active
Viewed 739 times
0
-
Why do you want to start Tomcat from Java? – Stefan Jul 23 '14 at 10:23
-
To have embedded tomcat with in my application for web apps. – cJ_ Jul 23 '14 at 10:29
2 Answers
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?
-
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