java.net.BindException: Address already in use: bind - 当用 jdk1.8 声明 tomcatx 时(不在调试模式下)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23425688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.net.BindException: Address already in use: bind - when stating tomcatx with jdk1.8 (not in debug mode)
提问by A4L
While setting up an environment for a tomcat project I ran into the exception
在为 tomcat 项目设置环境时,我遇到了异常
java.net.BindException: Address already in use: bind
The message is clear: the port is already used by some other process. So to troubleshoot I ran the following command
消息很明确:该端口已被其他进程使用。所以为了排除故障,我运行了以下命令
netstat -aon | find "<port>"
which returned nothing and thus suggests that the port is free for use by a new process.
它没有返回任何内容,因此表明该端口可供新进程免费使用。
Here is my statup script for tomcat, note here the use of the jdk 1.8
as JAVA_HOME
这里是我的tomcat的statup脚本,这里注意使用JDK的1.8
作为JAVA_HOME
@ECHO OFF
@SET JAVA_HOME=C:\Oracle\java\jdk1.8
@SET CATALINA_HOME=C:\<project>\apache-tomcat-8.0
@SET JAVA_OPTS=-Xmx512m -XX:MaxPermSize=128m
@CALL %CATALINA_HOME%\bin\catalina.bat run
At first I though it was an issue with tomcat8, so I have tryied with 7
and 6
too. Same issue.
起初,我虽然是与tomcat8一个问题,所以我tryied与7
和6
太。同样的问题。
After that, I stated to suspect the jdk, so I changed to 1.7
之后,我说怀疑jdk,所以我改成了 1.7
@SET JAVA_HOME=C:\Oracle\java\jdk1.7
and the problem was gone, with the same port numbers and the three tomcats.
问题消失了,端口号和三个tomcats相同。
So I checked my version of the jdk
所以我检查了我的 jdk 版本
Cmd-$> %JAVA_HOME%\bin\java.exe -version
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
and did an update (uninstall+install) to the latest version (at posting time)
并更新(卸载+安装)到最新版本(发布时)
Cmd-$> %JAVA_HOME%\bin\java.exe -version
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
So my questions are:
所以我的问题是:
- Am I mistaken somewhere and is there anything else I could check?
- Is this a bug in the oracle's jdk 1.8, and if yes is there a workaound for this?
- 我是不是在某个地方弄错了,还有什么我可以检查的吗?
- 这是 oracle 的 jdk 1.8 中的错误,如果是,是否有解决方法?
If there is any other informations I should provide, please comment.
如果有任何其他信息我应该提供,请发表评论。
EIDT
EIDT
Running the sample code in the bug report referenced by @StephenC fails on the first time with jdk 1.8
and behaves as one would expect with my actual jdk version of 1.7
.
运行@StephenC 引用的错误报告中的示例代码在第一次使用 jdk 时失败,1.8
并且行为与我的实际 jdk 版本的1.7
.
Cmd-$> %JAVA_HOME%\bin\java.exe -version
java version "1.7.0_17"
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
i.e. the first run is ok and the server responds correctly, the second fails with the expected BindExcxeption
.
即第一次运行正常并且服务器正确响应,第二次失败并出现预期的BindExcxeption
.
The 1.7 jdk which presumably reproduces the bug according to the bug report is
根据错误报告可能会重现错误的 1.7 jdk 是
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)
Running Tomcat 7 from eclipse using the Sysdeo Eclipse Tomcat Launcher plugingave no BindException, here are my setting for the plugin
使用Sysdeo Eclipse Tomcat Launcher 插件从Eclipse运行 Tomcat 7没有出现 BindException,这是我对插件的设置
I noticed though that the plugin runs javaw.exe
not java.exe
. I can't tell whether it makes a difference of not.
我注意到虽然该插件运行javaw.exe
没有java.exe
。我不知道这是否有区别。
Note that the tomxcat is ran in debug
mode (the checkbox Don't run Tomcat in debug mode
is not checked). If not ran in debug mode the exception comes.
请注意,tomxcat 在debug
模式下运行(Don't run Tomcat in debug mode
未选中复选框)。如果未在调试模式下运行,则会出现异常。
Also runnig tomcat from the script in debug mode using the jpda
switch
还可以使用jpda
开关在调试模式下从脚本运行 tomcat
@CALL %CATALINA_HOME%\bin\catalina.bat jpda run
seems to work around this issue.
似乎可以解决这个问题。
UPDATE
更新
Test on a 32 bit machine (+ 32 bit jdk and tomcat) were all successful with both jdk 1.8 and 1.7. Also the test class from the bug report behaves normally, i.e. BindException on second run.
在 32 位机器(+ 32 位 jdk 和 tomcat)上的测试在 jdk 1.8 和 1.7 上都成功。此外,错误报告中的测试类行为正常,即第二次运行时出现 BindException。
UPDATE 2
更新 2
The issue has survived a computer restart. Here is the output of the start process including the stacktrace, all other ports (AJP on 10009 and the server itself on 10005) are bound correctly:
该问题在计算机重新启动后仍然存在。这是启动过程的输出,包括堆栈跟踪,所有其他端口(10009 上的 AJP 和 10005 上的服务器本身)都已正确绑定:
Cmd-$> starttc.bat
Using CATALINA_BASE: "C:\<project>\apache-tomcat-8.0"
Using CATALINA_HOME: "C:\<project>\apache-tomcat-8.0"
Using CATALINA_TMPDIR: "C:\<project>\apache-tomcat-8.0\temp"
Using JRE_HOME: "C:\workspaces\Oracle\jdk1.8"
Using CLASSPATH: "C:\<project>\apache-tomcat-8.0\bin\bootstrap.jar;C:\<project>\apache-tomcat-8.0\bin\tomcat-juli.jar"
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0
05-May-2014 09:16:08.043 INFO [main] org.apache.catalina.core.AprLifecycleListener.init The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\workspaces\Oracle\jdk1.8\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\workspaces\Oracle\jdk1.6\bin;C:\oracle\product.2.0\dbhome_1\bin;C:\workspaces\Databases\oracle\product.2.0\dbhome_1\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Window
s\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Novell\ZENworks\bin;C:\Program Files\SlikSvn\bin;C:\Program Files\TortoiseSVN\bin;C:\bin;C:\Program Files\SlikSvn\bin;C:\private\bin;C:\Oracle\jdk1.6\bin;C:\MinGW\bin;C:\Misc\c_c++\process builder;C:\Apache\apache-maven-3\bin;C:\tools\database\sqlite\bin;.
05-May-2014 09:16:08.374 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-10090"]
05-May-2014 09:16:08.399 SEVERE [main] org.apache.coyote.AbstractProtocol.init Failed to initialize end point associated with ProtocolHandler ["http-nio-10090"]
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:414)
at sun.nio.ch.Net.bind(Net.java:406)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:351)
at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:683)
at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:456)
at org.apache.coyote.http11.AbstractHttp11JsseProtocol.init(AbstractHttp11JsseProtocol.java:120)
at org.apache.catalina.connector.Connector.initInternal(Connector.java:960)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
at org.apache.catalina.core.StandardService.initInternal(StandardService.java:567)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:826)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
at org.apache.catalina.startup.Catalina.load(Catalina.java:605)
at org.apache.catalina.startup.Catalina.load(Catalina.java:630)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:310)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:484)
05-May-2014 09:16:08.402 SEVERE [main] org.apache.catalina.core.StandardService.initInternal Failed to initialize connector [Connector[HTTP/1.1-10090]]
org.apache.catalina.LifecycleException: Failed to initialize component [Connector[HTTP/1.1-10090]]
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:106)
at org.apache.catalina.core.StandardService.initInternal(StandardService.java:567)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:826)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
at org.apache.catalina.startup.Catalina.load(Catalina.java:605)
at org.apache.catalina.startup.Catalina.load(Catalina.java:630)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:310)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:484)
Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
at org.apache.catalina.connector.Connector.initInternal(Connector.java:962)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
... 12 more
Caused by: java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:414)
at sun.nio.ch.Net.bind(Net.java:406)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:351)
at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:683)
at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:456)
at org.apache.coyote.http11.AbstractHttp11JsseProtocol.init(AbstractHttp11JsseProtocol.java:120)
at org.apache.catalina.connector.Connector.initInternal(Connector.java:960)
... 13 more
05-May-2014 09:16:08.404 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-10009"]
05-May-2014 09:16:08.453 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
05-May-2014 09:16:08.456 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 797 ms
05-May-2014 09:16:08.484 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
05-May-2014 09:16:08.484 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.5
05-May-2014 09:16:08.494 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\<project>\apache-tomcat-8.0\webapps\docs
05-May-2014 09:16:08.951 INFO [localhost-startStop-1] org.apache.catalina.util.SessionIdGenerator.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [151] milliseconds.
05-May-2014 09:16:08.985 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\<project>\apache-tomcat-8.0\webapps\examples
05-May-2014 09:16:09.786 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\<project>\apache-tomcat-8.0\webapps\host-manager
05-May-2014 09:16:09.828 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\<project>\apache-tomcat-8.0\webapps\manager
05-May-2014 09:16:09.883 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\<project>\apache-tomcat-8.0\webapps\ROOT
05-May-2014 09:16:09.916 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-10009"]
05-May-2014 09:16:09.937 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1489 ms
05-May-2014 09:17:30.430 INFO [Thread-5] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-10090"]
05-May-2014 09:17:30.431 INFO [Thread-5] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-10009"]
05-May-2014 09:17:30.498 INFO [Thread-5] org.apache.catalina.core.StandardService.stopInternal Stopping service Catalina
05-May-2014 09:17:30.551 INFO [Thread-5] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-nio-10009"]
05-May-2014 09:17:30.589 INFO [Thread-5] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-10090"]
05-May-2014 09:17:30.589 INFO [Thread-5] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-10090"]
05-May-2014 09:17:30.590 INFO [Thread-5] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["ajp-nio-10009"]
回答by Stephen C
I think you have been bitten by a Java 8 bug fix!
我认为您已经被 Java 8 错误修复所困扰!
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7179799
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7179799
Java on Linux and other (non-Windows) operating systems has always thrown an exception if you attempt to bind to a port that is already bound. For some reason, this was allowed in Java 6 and 7 on some Windows platforms. Oracle have deemed this to be a bug, and fixed it.
如果您尝试绑定到已绑定的端口,Linux 和其他(非 Windows)操作系统上的 Java 总是会引发异常。出于某种原因,这在某些 Windows 平台上的 Java 6 和 7 中是允许的。Oracle 认为这是一个错误,并修复了它。
Presumably, you were running 3 instances of Tomcat all listening on the same port as a "poor man's" load balancer. You are going to need to find another, more reliable way to do this.
据推测,您正在运行 3 个 Tomcat 实例,所有实例都在与“穷人”负载平衡器相同的端口上进行侦听。您将需要找到另一种更可靠的方法来做到这一点。
UPDATE
更新
Just to make it clear, you should not attemptto run two Tomcat instances on the same port. Especially a production server and a test server.
为了清楚起见,您不应尝试在同一端口上运行两个 Tomcat 实例。特别是生产服务器和测试服务器。
If you do manage to get it "working", then it will be uncertain which instance will get requests sent to the IP/port. Some may go to the production server, and some to the test server. In short, your test server will interferewith your production server.
如果您确实设法让它“工作”,那么将不确定哪个实例将请求发送到 IP/端口。有些可能会转到生产服务器,有些可能会转到测试服务器。简而言之,您的测试服务器会干扰您的生产服务器。
A much better idea is to configure your test Tomcat instance to listen on a different port and/or IP address to the production instance.
一个更好的主意是配置您的测试 Tomcat 实例以侦听生产实例的不同端口和/或 IP 地址。
回答by idrosid
I have the impression that finddoes not work as expected. Try:
我的印象是find没有按预期工作。尝试:
netstat -aon | findstr "80"
or just
要不就
netstat -aon
to make sure that port 80 is free
确保端口 80 是空闲的
回答by user207421
Tomcat uses more than one port. You're just assuming that port 80 is the problem. Have a look through server.xml and check allthe ports mentioned. Your Tomcat may be running on port 8080 for example, or it may be the shutdown port, which is normally 8005, or the HTTPS port (443 or 8443), ...
Tomcat 使用多个端口。您只是假设端口 80 是问题所在。查看 server.xml 并检查提到的所有端口。例如,您的 Tomcat 可能在端口 8080 上运行,或者它可能是关闭端口,通常是 8005,或 HTTPS 端口(443 或 8443),...