• Post Reply Bookmark Topic Watch Topic
  • New Topic

Running Tomcat From Java Code

 
Arpit Tayal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to run tomcat from my java code. It works fine and start the tomcat perfectly but immediately it will shutdown the current or started thread.

Problem might be thread dies after execution and will stops JVM execution.

Following is the code snippet. Required bootstrap.jar of tomcat for running Code.

import org.apache.catalina.startup.Bootstrap;

public class TomcatLauncher implements Runnable {


public Bootstrap bootStrap;
public Boolean readyToExit = false;
static Thread t ;
private Thread shutdownThread;
static
{
TomcatLauncher tl = new TomcatLauncher();
t = new Thread(tl);
t.setDaemon(true);
}

public static void main(String[] args)
{
t.start();

}
public void run() {
//registerShutdownHook();
startTomcat();
showStartPage();
}
private void startTomcat()
{
try
{
log("Starting Tomcat...");
bootStrap = new Bootstrap();
bootStrap.init();
bootStrap.start();
log("...Tomcat started.");
// Thread.sleep(1555555);
}
catch (Throwable t)
{
t.printStackTrace();
}
}
private void log(String msg) {
// FIXME you will probably want to make this write to a file
System.out.println(msg);
}
}

Please help ...........
Thanks

Arpit
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're making the TomcatLauncher thread a daemon, which means it will exit (as will the JVM) if there are no other non-daemon threads running. Is that the thread that runs Tomcat?
 
Arpit Tayal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes my intention of making thread as daemon thread that it can not be shutdown by any user thread. Daemon thread is running tomcat still it shutdown the Tomcat. May be due to JVM shutdown . Can we fork a new JVM or any other idea
 
Henry Wong
author
Sheriff
Posts: 22518
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arpit Tayal wrote:Yes my intention of making thread as daemon thread that it can not be shutdown by any user thread. Daemon thread is running tomcat still it shutdown the Tomcat. May be due to JVM shutdown . Can we fork a new JVM or any other idea



I think you are confusing the definition of "daemon" thread -- perhaps with the unix definition of daemon process. In Java, a daemon thread doesn't mean it can't be shut down by another thread. A daemon thread just means that the thread is not a user thread, and the JVM automatically terminates when there are no user threads.

Henry
 
Arpit Tayal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the solution. The problem is that Daemon thread automatically Shutdown when JVM shutdown. So for the Solution i made a applet and from applet i starts the daemon thread.So it will not shutdown until i have applet running
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arpit Tayal wrote:The problem is that Daemon thread automatically Shutdown when JVM shutdown. So for the Solution i made a applet and from applet i starts the daemon thread.So it will not shutdown until i have applet running

You have an *applet* that starts a servlet container? Ugh.

If the problem is the daemon nature of a thread, then why do you make it a daemon?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!