解決tomcat7安裝後啟動時一閃而退的bug
問題描述
從apache官網下載64位tomcat7.0.90安裝包檔案解壓到個人電腦D盤後,進入tomcat伺服器的bin目錄雙擊startup.bat windows批處理檔案啟動tomcat伺服器時,出現閃退。
解決辦法
關於tomcat伺服器啟動出現閃退的問題,網上已有關於解決此類問題的描述,大部分都是因為未配置JAVA_HOME, JRE_HOME和CATALINA_HOME等環境變數而引起的。關於如何配置 3個環境變數,很多關於解決tomcat伺服器啟動閃退的部落格甚至百度經驗上都有,此處就不贅述了。不懂的同學可以參考這篇部落格
但是也有特殊情況,就是你配置了上述環境變數後再此啟動tomcat伺服器還出現閃退的Bug, 這個時候我們就需要檢視日誌找到問題產生的原因。進入tomcat根目錄的log資料夾如下圖所示:
找到最近產生的catalina.+日期.txt檔案,使用nodepad++開啟檢視內容,並把日誌資訊貼上如下
九月 09, 2018 1:52:31 上午 org.apache.catalina.core.AprLifecycleListener init
警告: The APR based Apache Tomcat Native library failed to load. The error reported was [D:\tomcat7.0\apache-tomcat-7.0.90\bin\tcnative-1.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform]
java.lang.UnsatisfiedLinkError: D:\tomcat7.0\apache-tomcat-7.0.90\bin\tcnative-1.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.apache.tomcat.jni.Library.<init>(Library.java:42)
at org.apache.tomcat.jni.Library.initialize(Library.java:178)
at org.apache.catalina.core.AprLifecycleListener.init(AprLifecycleListener.java:200)
at org.apache.catalina.core.AprLifecycleListener.isAprAvailable(AprLifecycleListener.java:108)
at org.apache.catalina.connector.Connector.setProtocol(Connector.java:592)
at org.apache.catalina.connector.Connector.<init>(Connector.java:66)
at org.apache.catalina.startup.ConnectorCreateRule.begin(ConnectorCreateRule.java:62)
at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1303)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1576)
at org.apache.catalina.startup.Catalina.load(Catalina.java:628)
at org.apache.catalina.startup.Catalina.load(Catalina.java:679)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:253)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:427)
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: Server version: Apache Tomcat/7.0.90
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: Server built: Jul 2 2018 17:05:37 UTC
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: Server number: 7.0.90.0
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: OS Name: Windows 10
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: OS Version: 10.0
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: Architecture: x86
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: Java Home: C:\Program Files (x86)\Java\jre1.8.0_161
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: JVM Version: 1.8.0_161-b12
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: JVM Vendor: Oracle Corporation
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: CATALINA_BASE: D:\tomcat7.0\apache-tomcat-7.0.90
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: CATALINA_HOME: D:\tomcat7.0\apache-tomcat-7.0.90
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: Command line argument: -Djava.util.logging.config.file=D:\tomcat7.0\apache-tomcat-7.0.90\conf\logging.properties
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: Command line argument: -Dignore.endorsed.dirs=
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: Command line argument: -Dcatalina.base=D:\tomcat7.0\apache-tomcat-7.0.90
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: Command line argument: -Dcatalina.home=D:\tomcat7.0\apache-tomcat-7.0.90
九月 09, 2018 1:52:32 上午 org.apache.catalina.startup.VersionLoggerListener log
資訊: Command line argument: -Djava.io.tmpdir=D:\tomcat7.0\apache-tomcat-7.0.90\temp
九月 09, 2018 1:52:32 上午 org.apache.coyote.AbstractProtocol init
資訊: Initializing ProtocolHandler ["http-bio-8080"]
九月 09, 2018 1:52:32 上午 org.apache.coyote.AbstractProtocol init
嚴重: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"]
java.net.BindException: Address already in use: JVM_Bind <null>:8080
at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:413)
at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:728)
at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:452)
at org.apache.coyote.http11.AbstractHttp11JsseProtocol.init(AbstractHttp11JsseProtocol.java:119)
at org.apache.catalina.connector.Connector.initInternal(Connector.java:978)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
at org.apache.catalina.core.StandardService.initInternal(StandardService.java:560)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:840)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
at org.apache.catalina.startup.Catalina.load(Catalina.java:654)
at org.apache.catalina.startup.Catalina.load(Catalina.java:679)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:253)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:427)
Caused by: java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at org.apache.tomcat.util.net.DefaultServerSocketFactory.createSocket(DefaultServerSocketFactory.java:49)
at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:400)
... 17 more
通過檢視以上日誌資訊,我們發現這個bug主要由兩處錯誤引起,第一處錯誤:
The APR based Apache Tomcat Native library failed to load. The error reported was [D:\tomcat7.0\apache-tomcat-7.0.90\bin\tcnative-1.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform]
java.lang.UnsatisfiedLinkError: D:\tomcat7.0\apache-tomcat-7.0.90\bin\tcnative-1.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform
原來是因為我的jdk平臺是32位的,而tomcat7.0卻是64位的,導致tomcat啟動過程中載入native library失敗。通過最開始的dos控制檯資訊我們發現tomcat啟動時用到了jre,而我的環境變數配置的jre路徑卻是32位的(C:\Program Files (x86)目錄下的安裝軟體都屬於32bit位的軟體)。原來的jdk和jre環境變數配置如下圖所示
將jdk和jre的環境變數值加入到path值中,原來已配置好(如下圖,本人採用的是win 10系統)
因此我的解決辦法是安裝64bit位的jdk,然後重新配置jdk和jre環境變數即可,配置後的jdk和jre環境變數如下圖所示:
然後我們再來看第二處錯誤: java.net.BindException: Address already in use: JVM_Bind <null>:8080
這說明8080埠已被佔用,這是由於我之前安裝過32位的tomcat8.0並啟動成功過導致的,這個問題我們可以通過tomcat\conf目錄下的server.xml伺服器的配置檔案修改啟動埠號來解決,tomcat伺服器啟動時預設的連線埠號是8080,現在我們把它改為8088就不會出現埠被佔用的衝突了
<Connector port="8088" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
重啟電腦後進入tomcat\bin目錄,雙擊startup.bat檔案不再出現閃退,並顯示tomcat伺服器啟動成功,如下圖所示。
進入瀏覽器輸入http://localhost:8088並回車,進入如下介面說明tomcat伺服器確實啟動成功,問題到此得以解決