2011-03-16 109 views
2

我使用tomcat作爲我的web服務器。我的應用程序需要用戶點擊編輯按鈕後重新啓動tomcat服務器。對於我已經寫了代碼我控制器上如下:Tomcat問題:java.net.BindException:地址已經在使用<null>:8080 at org.apache.tomcat.util.net.JIoEndpoint.init(JIoEndpoint.java:549)

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 }}}}}}}}}}}}}}"); 
} 

我的Tomcat版本6.0.32,雖然乳寧我的應用程序關閉它Tomcat服務器proparly並開始與異常

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) 

請幫助我解決

預先感謝

+0

http://stackoverflow.com/questions/5310945/deployment-errorstarting-of-tomcat-failed-the-server-port-8080-is-already-in-us/5310993#5310993 – 2011-03-16 05:53:04

回答

3

該代碼雖然難以閱讀,但似乎並未等待關機發生。我懷疑當您嘗試重新啓動時,它仍處於關閉過程中。當然,異常表明它仍在運行,因爲它的http端口仍在使用中。要暫時追蹤問題,請嘗試在關機和啓動之間進行休眠,以查看是否可以解決問題。

+0

我試着給1000毫秒的睡眠,但異常是相同的 – Chitresh 2011-03-16 05:44:04

+0

嘗試10或100次。某些servlet可能需要做一些不重要的工作(例如,在服務器關閉之前,「完成請求」並將狀態刷新到磁盤)。 – 2011-03-16 06:25:14

+0

感謝斯蒂芬和梅爾夫因爲你的幫助。你的幫助對我有用。 – Chitresh 2011-03-16 07:52:15

2

其他一些應用程序已經在偵聽端口8080殺死該進程,然後運行您的服務器。一次只能附加一個進程。查找已經綁定到該端口的進程是特定於操作系統的。

+0

但正如我所說所有關於用戶操作,所以我如何在運行時殺死進程。首先當我運行我的應用程序服務器啓動端口8080.但在用戶編輯操作我重新啓動我的服務器,然後這個異常即將到來,在這之間我可以殺死進程。有計時器任務正在後臺運行任何計時器任務的問題 – Chitresh 2011-03-16 05:37:34

+0

它的計時器任務是一個守護進程線程,它不應該阻止JVM關閉。如果不 ...以及您的servlet需要傾聽相關的生命週期事件並以適當的方式關閉計時器任務。 – 2011-03-16 06:28:38

0

我面臨的BindException的8080端口上同樣的問題我按照這些步驟:

netstat -tulpn 

它列出了所有的進程ID,他們被監聽的端口。然後我在列表中搜索端口8080,然後查找其PID。該PID爲1654然後我用以下命令殺死進程:

kill -9 1654 

然後,我開始Tomcat服務器,它工作得很好。希望能幫助到你! :)

0

我也有這個問題,在我的情況下,在嘗試配置Tomcat時,我創建了2個「引擎」。我曾在./conf/server.xml這樣一行:

<Engine name="Catalina" defaultHost="localhost">

我改變了這種設置,在更早的server.xml中匹配的設置如下:

<Engine name="Standalone" defaultHost="localhost">

我沒有意識到的是,這在./conf/Catalina/和./conf/Standalone/中創建了兩個獨立的配置。這兩個相互衝突,給了我與本文相同的錯誤。

我關閉了tomcat,刪除了./conf/Catalina/和./conf/Standalone子目錄,並重新啓動。 Tomcat創建了新的,沒有更多的衝突!

相關問題