windows 环境下 eclipse + maven + tomcat 的 hello world 创建和部署

本文详细记录了使用Eclipse、Maven和Tomcat搭建Helloworld项目的全过程,包括解决过程中遇到的各种典型问题,如环境变量配置、依赖管理、项目编译与部署等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要记录自己一个新手用 eclipse + maven + tomcat 搭建 hello world 的过程,以及遇到的问题。讲真都是自己通过百度和谷歌一步步搭建的项目,没问过高手,也没高手可问,由于之前只接触过 .net 和 android 的开发,要知道.net 的开发环境 vs 和 android 的 studio 都是开箱即用,没搭建过 java 的开发环境,遇到几个好致命的问题,花了两天时间不断的google、baidu、尝试才处理掉,差点放弃了。难以想象,如果没有互联网,得交多少学费才能学会搭建一个项目框架。

以下图的项目名 “hellowork” 单词拼错了,应该是 helloworld , 但不影响演示,就不做修改,重新截图麻烦呀,理解理解。

配置开发环境

1) 下载 JDK 安装;
2) 下载 Tomcat 安装;
3) 打开环境变量配置界面
488490-20170505180428164-1906567188.png

4) 添加以下环境变量:

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 安装所在的路径,还有标点符号也要;

5) 安装Eclipse 安装, 下载 j2ee 版本 ;

创建 Hello World 项目

1) 在 eclipse 打开菜单 File -> New -> Maven Project, 将会出现以下界面

488490-20170506093021273-1976589285.png

直接点 Next

488490-20170506093241039-744452771.png

选择 "maven-archetypes-webapp", 然后 Next
488490-20170506093828648-751398812.png

输入 group id, artifact id, 然后 Finish

488490-20170506093959898-773746596.png

项目创建完毕,但很多情况下,还有其他问题,导致项目还不能编译通过,下面是我遇到过的问题及解决方案;

项目 Error 及解决方案

错误一: The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path index.jsp, 如下图

488490-20170506094654632-80763629.png

解决方案:

1) 添加运行环境: eclipse -> Window -> Preferences, 如下图添加自己安装的 tomcat 版本
488490-20170506115022195-676678832.png

2) 点击 Next, 如下图,指定 Tomcat 所在的路径
488490-20170506115407398-1500991164.png

3) 指定 JRE, 在下拉中选择安装的 jre 版本

488490-20170506141103289-1887610168.png

4) 如果下拉中没有 jdk 文件夹下的 jre,则点击“Installed JREs...”, 添加已安装的 JRE

488490-20170506141320476-988242646.png

选择 Standard VM, 点 Next,指点 JRE 所在的路径,然后 Finish
488490-20170506141836773-1753924062.png

5) 添加成功后
488490-20170506142128711-2067922818.png

6) 然后再右击项目 -> Properties, 进行以下操作
488490-20170506100744507-1664696735.png

选择 Apache Tomcat v8.5, (v8.5自己安装的 tomcat 版本)
488490-20170506102019414-841600795.png

然后再到 Order and Export 栏位勾选刚添加的库: Apache Tomcat v8.5[Apache Tomcat v8.5]

488490-20170506102507976-70632331.png

确定后,错误提示消失了

488490-20170506102602914-1566750529.png

运行项目

1) 运行前,先添加服务器
488490-20170506102925242-2083659272.png
点击上图的“No servers are available. Click this link to create a new server”,

488490-20170506103204179-1783168385.png

选择自己安装的 tomcat 版本,我的是Tomcat v8.5 Server, 然后点击 Finish, 可看到在左边的工程里多出了一个 Servers 项目,同样在底部的 Servers 看到刚才添加的服务器

488490-20170506103757242-612075843.png

2) 右击项目“hellowork”, 选择 Run as -> Run On Server, 弹出以下界面,点击 Finish
488490-20170506103956851-352072188.png

正常在浏览器看到 Hello World。 到这里我发现自己打错了 hello world, 项目名写成了 hellowork [尴尬]

488490-20170506110328539-220729958.png

3) 有可能出现端口被占用的错误"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 停掉

488490-20170506111509914-1355250923.png

4) 如果问题还在,进入 cmd 输入 netstat -ao 查看系统端口被占用的信息

488490-20170506105409273-1034251454.png
上图看到 8005 端口被 13116 这进程占用了, 打开“任务管理器”找到该进程

488490-20170506105314632-1171256079.png
原来是 javaw 占用了,把它结束掉,再运行就可以了。

编译项目

1) 右击 helloworld 项目,选择 Run As -> Maven Build, 会弹出以下界面

488490-20170506154725445-102198659.png

在 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 显示的信息
488490-20170506154018289-1341503176.png

部署到 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/

488490-20170506154531664-1759780040.png

转载于:https://www.cnblogs.com/grissom007/p/6814359.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值