Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When I run Apache Tomcat startup.bat file manually the cmd console stays open, when I run the same in java code, the console closes. (I am trying to automate some processes in my project). For Windows it seems when Tomcat is run as an application the console must remain open after the .bat runs.

How can I make this happen for my java code? Thanks

share|improve this question
1  
cmd /k batchfile will leave cmd open, but that's most likely not what you want. I would guess it's a problem with paths/running directory causing startup to fail (as it cannot execute command(s) in batch). I would also suggest to change the title to better explain your specific issue –  wmz Aug 3 '12 at 10:38
    
could you detail what you mean by 'manually'? How exactly you run this batch file? –  wmz Aug 3 '12 at 19:58

2 Answers 2

up vote 1 down vote accepted

Startup.bat is dependent on two env variables: CATALINA_HOME and JRE_HOME (or JAVA_HOME).
You must have either JRE_HOME or JAVA_HOME set, otherwise you would not be able to run startup by double clicking.
This leaves us with CATALINA_HOME. Startup script will try to guess it if it's not set, but this works only if it's executed from it's current directory (or it's parent to be precise). If you try to run it from other directory and CATALINA_HOME is not set it will fail (with a message about it).
If that's your issue you should see similar message at the top of cmd window:

The CATALINA_HOME environment variable is not defined correctly This environment variable is needed to run this program

Another issue you may run into is when your path contains spaces. It looks yours does not, but it still may be usefull to others, so I will include this piece of info.
When your command includes spaces, you need to quote your start command, right? This should do: "cmd.exe /c start \"my path to tomcat bin/startup.bat\""
Wrong. You end up glaring at command window titled my path to tomcat bin/startup.bat
start takes first quoted argument as title... how nice. Correct form is:
"cmd.exe /c start \"\" \"my path to tomcat bin/startup.bat\""

But wait

both of those issues will actually stop Tomcat window from appearing at all - you say it closes, so it does start and then dies. Hmm... this is most likely because there is already a process listening on port 8080. Maybe your java IDE starts Tomcat? Or starts another server listening on 8080? If's that's the cause, you should see similar line in Tomcat log (they are stored in CATALINA_HOME/logs):

java.net.BindException: Address already in use: JVM_Bind

If that's your issue, than you either need to stop first instance, or reconfigure your server processes so each listens on different port.

Note: I used following to start Tomcat from java, works fine for me:

    Runtime r=Runtime.getRuntime();
    r.exec("cmd.exe /c start startup.bat",          //path to executable
            null,                                   // env vars, null means pass parent env
            new File("my path to tomcat/bin")); // working directory 
share|improve this answer
    
Thanks wmz it worked. You have been a big help. –  Learner Aug 7 '12 at 23:33
    
Glad to know, could you accept the answer then (check mark next to voting arrows)? –  wmz Aug 8 '12 at 11:30

Check the file properties of the .bat file (right click on it), there should be the option to keep the console opened. Last tab as far as I remember.

share|improve this answer
    
Thanks for your responses, @wmz I tried that and it did not work;@kenorb that option is not available,using Windows 7. –  Learner Aug 3 '12 at 16:54
    
@wmz I mean simply double clicking on startup.bat. When I put String command = "cmd /C start C:/Users/~/startup.bat" (~ is the rest of path to file) in java, it runs but the console closes afterwards. I need it to stay open so that the Tomcat server continues running. Sorry for not being clear before. –  Learner Aug 4 '12 at 8:46
    
@Learner I'm not notified of comments on this post even if you included my name... Anyway, I posted an answer for you –  wmz Aug 5 '12 at 0:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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