主要記錄自己一個新手用 eclipse + maven + tomcat 搭建 hello world 的過程,以及遇到的問題。講真都是自己通過百度和谷歌一步步搭建的項目,沒問過高手,也沒高手可問,由於之前只接觸過 .net 和 android 的開發,要知道.net 的開發環境 vs 和 android 的 studio 都是開箱即用,沒搭建過 java 的開發環境,遇到幾個好致命的問題,花了兩天時間不斷的google、baidu、嘗試才處理掉,差點放棄了。難以想象,如果沒有互聯網,得交多少學費才能學會搭建一個項目框架。
以下圖的項目名 “hellowork” 單詞拼錯了,應該是 helloworld , 但不影響演示,就不做修改,重新截圖麻煩呀,理解理解。
配置開發環境
JAVA_HOME:
C:\Program Files\Java\jdk1.8.0_131
CLASSPATH:
.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;C:\Program Files\Apache Software Foundation\Tomcat 8.5\lib\servlet-api.jar;
在 PATH 后面添加
;%JAVA_HOME%\bin;
注意 C:\Program Files\Java 是你的 JDK 安裝所在路徑, C:\Program Files\Apache Software Foundation\Tomcat 8.5 是 Tomcat 安裝所在的路徑,還有標點符號也要;
- 安裝Eclipse 安裝, 下載 j2ee 版本 ;
創建 Hello World 項目
- 在 eclipse 打開菜單 File -> New -> Maven Project, 將會出現以下界面
直接點 Next
選擇 "maven-archetypes-webapp", 然后 Next
輸入 group id, artifact id, 然后 Finish
項目創建完畢,但很多情況下,還有其他問題,導致項目還不能編譯通過,下面是我遇到過的問題及解決方案;
項目 Error 及解決方案
錯誤一: The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path index.jsp, 如下圖
解決方案:
-
添加運行環境: eclipse -> Window -> Preferences, 如下圖添加自己安裝的 tomcat 版本
-
點擊 Next, 如下圖,指定 Tomcat 所在的路徑
-
指定 JRE, 在下拉中選擇安裝的 jre 版本
- 如果下拉中沒有 jdk 文件夾下的 jre,則點擊“Installed JREs...”, 添加已安裝的 JRE
選擇 Standard VM, 點 Next,指點 JRE 所在的路徑,然后 Finish
-
添加成功后
-
然后再右擊項目 -> Properties, 進行以下操作
選擇 Apache Tomcat v8.5, (v8.5自己安裝的 tomcat 版本)
然后再到 Order and Export 欄位勾選剛添加的庫: Apache Tomcat v8.5[Apache Tomcat v8.5]
確定后,錯誤提示消失了
運行項目
- 運行前,先添加服務器
點擊上圖的“No servers are available. Click this link to create a new server”,
選擇自己安裝的 tomcat 版本,我的是Tomcat v8.5 Server, 然后點擊 Finish, 可看到在左邊的工程里多出了一個 Servers 項目,同樣在底部的 Servers 看到剛才添加的服務器
- 右擊項目“hellowork”, 選擇 Run as -> Run On Server, 彈出以下界面,點擊 Finish
正常在瀏覽器看到 Hello World。 到這里我發現自己打錯了 hello world, 項目名寫成了 hellowork [尷尬]
- 有可能出現端口被占用的錯誤"Several ports (8005, 8080, 8009) required by Tomcat v8.5 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the other process or change the port number(s).", 不能同時運行兩個 tomcat, 先把運行中的 tomcat 停掉
- 如果問題還在,進入 cmd 輸入 netstat -ao 查看系統端口被占用的信息
上圖看到 8005 端口被 13116 這進程占用了, 打開“任務管理器”找到該進程
原來是 javaw 占用了,把它結束掉,再運行就可以了。
編譯項目
- 右擊 helloworld 項目,選擇 Run As -> Maven Build, 會彈出以下界面
在 Goals 中輸入 clean package , 然后點擊 Run, 首次運行可能有些庫要下載,如果網速慢,會等上 20 分鍾;
如果報以下錯誤:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project hellowork: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test failed: Unable to load the mojo 'test' in the plugin 'org.apache.maven.plugins:maven-surefire-plugin:2.12.4'. A required class is missing: org/apache/maven/plugin/surefire/SurefireReportParameters
[ERROR] -----------------------------------------------------
[ERROR] realm = plugin>org.apache.maven.plugins:maven-surefire-plugin:2.12.4
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/plugins/maven-surefire-plugin/2.12.4/maven-surefire-plugin-2.12.4.jar
[ERROR] urls[1] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/surefire/surefire-booter/2.12.4/surefire-booter-2.12.4.jar
[ERROR] urls[2] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/surefire/surefire-api/2.12.4/surefire-api-2.12.4.jar
[ERROR] urls[3] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/surefire/maven-surefire-common/2.12.4/maven-surefire-common-2.12.4.jar
[ERROR] urls[4] = file:/C:/Users/Grissom/.m2/repository/org/apache/commons/commons-lang3/3.1/commons-lang3-3.1.jar
[ERROR] urls[5] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/shared/maven-common-artifact-filters/1.3/maven-common-artifact-filters-1.3.jar
[ERROR] urls[6] = file:/C:/Users/Grissom/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.8/plexus-utils-3.0.8.jar
[ERROR] urls[7] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/reporting/maven-reporting-api/2.0.9/maven-reporting-api-2.0.9.jar
[ERROR] urls[8] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/plugin-tools/maven-plugin-annotations/3.1/maven-plugin-annotations-3.1.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import from realm ClassRealm[maven.api, parent: null]]
[ERROR]
[ERROR] -----------------------------------------------------: org.apache.maven.plugin.surefire.SurefireReportParameters
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException
解決方案: 在 pom.xml 中的 build 改為:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
即跳過測試。
編譯成功后,Console 顯示的信息
部署到 Tomcat
編譯好的 war 文件默認放在項目的 target 文件夾下(上圖中的 [INFO] Building war: C:\Users\Grissom\Documents\workspace\hellowork\target\hellowork-0.0.1-SNAPSHOT.war),把它考到 Tomcat 的 \webapps 目錄下,並改名成 hellowork.war , 重啟 Tomcat,在瀏覽器訪問 http://localhost:8080/hellowork/