Reputation: 3092
I am using tomcat as my webserver. i have application which needs a tomcat server restart after click of edit button by user. for that i have written a code on my controller as below:
if (updateResult ==1)
{
//String command = "/home/sunil_shiwankar/Tempo/apache-tomcat-6.0.32/startup.sh";//for linux use .sh
String command = this.getClass().getResource("/").getPath();
System.out.println("{{{{{{{{{{{{{{{{{{{{{{{{{{{"+ command);
command = command.replace(command.substring(command.lastIndexOf("webapps"), command.length()), "");
System.out.println("{{{{{{{{{{{{{{{{{{{{{{{{{{{ AFTER MODIFYING COMMAND IS "+ command);
try
{
String shutDownCommand = command+"bin/shutdown.sh";
Process child = Runtime.getRuntime().exec(shutDownCommand);
System.out.println("{{{{{{{{{{{{{{{{{{{{{{{{{{{ SHUTDOWN TOMCAT SUCCESSFULLY }}}}}}}}}}}}}}");
} catch (Exception e)
{
String inExceptionStartCommand = command+"bin/startup.sh";
Process child = Runtime.getRuntime().exec(inExceptionStartCommand);
System.out.println("{{{{{{{{{{{{{{{{{{{{{{{{{{{ TOMCAT STARTED WITH EXCEPTION SUCCESSFULLY }}}}}}}}}}}}}}");
e.printStackTrace();
}
String normalStartCommand = command+"bin/startup.sh";
Process child = Runtime.getRuntime().exec(normalStartCommand);
System.out.println("{{{{{{{{{{{{{{{{{{{{{{{{{{{ TOMCAT STARTED WITOUT EXCEPTION SUCCESSFULLY }}}}}}}}}}}}}}");
}
my tomcat version 6.0.32 and while runing my application it shutdown tomcat server proparly and starts with an exception
java.net.BindException: Address already in use <null>:8080
at org.apache.tomcat.util.net.JIoEndpoint.init(JIoEndpoint.java:549)
at org.apache.coyote.http11.Http11Protocol.init(Http11Protocol.java:176)
at org.apache.catalina.connector.Connector.initialize(Connector.java:1022)
at org.apache.catalina.core.StandardService.initialize(StandardService.java:703)
at org.apache.catalina.core.StandardServer.initialize(StandardServer.java:838)
at org.apache.catalina.startup.Catalina.load(Catalina.java:538)
at org.apache.catalina.startup.Catalina.load(Catalina.java:562)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:261)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.net.BindException: Address already in use
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:365)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at java.net.ServerSocket.<init>(ServerSocket.java:141)
at org.apache.tomcat.util.net.DefaultServerSocketFactory.createSocket(DefaultServerSocketFactory.java:50)
at org.apache.tomcat.util.net.JIoEndpoint.init(JIoEndpoint.java:538)
... 12 more
16 Mar, 2011 10:17:43 AM org.apache.catalina.core.StandardService initialize
SEVERE: Failed to initialize connector [Connector[HTTP/1.1-8080]]
LifecycleException: Protocol handler initialization failed: java.net.BindException: Address already in use <null>:8080
at org.apache.catalina.connector.Connector.initialize(Connector.java:1024)
at org.apache.catalina.core.StandardService.initialize(StandardService.java:703)
at org.apache.catalina.core.StandardServer.initialize(StandardServer.java:838)
at org.apache.catalina.startup.Catalina.load(Catalina.java:538)
at org.apache.catalina.startup.Catalina.load(Catalina.java:562)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:261)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
please help me to resolve
Thank in advance
Upvotes: 3
Views: 31717
Reputation: 41
I had this problem as well, and in my case, in attempting to configure Tomcat, I created 2 "engines". I had a line like this in ./conf/server.xml:
<Engine name="Catalina" defaultHost="localhost">
I changed this setting to match a the setting in an earlier server.xml to this:
<Engine name="Standalone" defaultHost="localhost">
What I didn't realize was that this created two, separate configurations in ./conf/Catalina/ and ./conf/Standalone/. These two conflicted and gave me the same error as in this post.
I shutdown tomcat, removed both ./conf/Catalina/ and ./conf/Standalone subdirectories, and re-started. Tomcat created the new one, and no more conflicts!
Upvotes: 0
Reputation: 1472
I faced the same problem of BindException on port 8080. I followed these steps:
netstat -tulpn
It listed down all the process IDs and the ports they were listening. then I searched for port 8080 in the list and then looked for its PID. The PID was 1654. Then I killed the process using the following command:
kill -9 1654
Then I started the tomcat server and it worked fine. Hope it helps! :)
Upvotes: 3
Reputation: 2201
That code, although hard to read, appears to not be waiting for the shutdown to happen. I suspect it's still in the process of shutting down when you try to start it again. Certainly, the exception indicates it's still running, because its http port is still in use. To track the problem down temporarily, try putting a sleep in between the shutdown and startup to see if it fixes the problem.
Upvotes: 4
Reputation: 24791
Some other application is already listening on port 8080. Kill that process and then run your server. Only one process can attach to a process at a time. Finding the process which is already bound to that port is OS specific.
Upvotes: 3