Windows 平台 JavaEE 基本开发环境搭建

1 安装JDK

1.1下载

地址 http://www.oracle.com/technetwork/java/javase/downloads/index。

直接官网下载安装,注意版本对应,如果是老机器,要注意下载以应的x86而不是x64版本。

java ldle安装_maven


java ldle安装_apache_02

1.2 安装 JDK

运行安装包,一路 next, 中途可选择安装路径和内容,如果只是纯开发,可以不装 Public JRE。

java ldle安装_tomcat_03

1.3 设置系统变量

在用户变量处新增一个 JAVA_HOME 变量,值为 JDK 的安装路径;

在用户变量的 Path 变量的值新增 %JAVA_HOMR%/bin。

java ldle安装_tomcat_04

1.4 测试

运行 cmd, 输入 java -version, 如果能正确显示相关信息,表明安装成功。

java ldle安装_apache_05

2. 安装 Eclipse

2.1 下载 Eclipse

地址 https://www.eclipse.org/downloads/eclipse-packages/

要注意下载的版本

是进行javaee下载,因此是带了jee插件的eclipse

java ldle安装_apache_06

2.2 安装 Eclipse

把压缩包的内容解压到一个你平常放软件的路径,最好不要有中文、空格或特殊字符,然后新建一个 eclipse.exe 的快捷方式方便运行就可以了。

java ldle安装_tomcat_07

2.3 Eclipse 简单配置

第一次运行 Eclipse 会让你设定一个工作空间的路径,选择一个你打算存放项目的路径,然后还可以勾选默认,这样就不用每次运行都要选择一次工作空间了。

java ldle安装_apache_08

2.4 设置

  • 点击 Window - Preferences - General - Workspace, 把 Text file encoding 改为 UTF-8, 把 New text file line delimiter 改为 unix
  • Window - Preferences - Java - Formatter, 点击 Edit…, Tab policy 改为 Space only, Indentation size 和 Tab size 改为 2, 自定义 Profile name, Apply
  • Window - Preferences - XML - XML Files - Editor, 选择 Indent using spaces, Indentation size 改为 2
  • 检查 Window - Preferences - Java 里的 Compiler 和 Installed JREs, 确认 JDK 已被正确配置。

2.5 测试

最后写个简单的 Hello World 跑一下,没问题就可以了。

2.6 其他个人习惯设置

2.6.1 外置浏览器,建议用chrome方便前端调试

java ldle安装_maven_09

2.6.2 编码统一设置

java ldle安装_apache_10


java ldle安装_java ldle安装_11


统一都设置为UTF-8

2.6.3 Eclipse将换行符统一设置成Linux风格

java ldle安装_apache_12


然后Windows -> Preference -> General -> Editors -> Text Editors:

java ldle安装_maven_13


然后勾选Show whitespace characters复选框。

OK,这样结合之前对Git的设置,这样就可以保证你的代码不会因为换行符转换给你添麻烦啦。不断积累软件的使用技巧,让我们的工具变得更智能,更懂你的心。

3. 安装 Tomcat

3.1 下载 Tomcat

地址 http://tomcat.apache.org/

https://tomcat.apache.org/download-90.cgi 国内,建议用镜像站点的资源,效率要高一些。

java ldle安装_java ldle安装_14

3.2 安装 Tomcat

把压缩包的内容解压到一个你喜欢的路径,不要有中文、空格或特殊字符,路径不要深。

3.2.1系统配置-环境变量

在系统变量中添加以下变量

  (1)TOMCAT_HOME,该变量指向解压文件的路径,该目录下有lib、bin等文件夹。添加方法如下:

  点击环境变量下的“新建”,在“变量名”中填写“TOMCAT_HOME”,在“变量值”中填写解压文件的路径,

  D:\Tomcat\apache-tomcat-9.0.0.M1-windows-x64\apache-tomcat-9.0.0.M1

  (后面没有分号)然后点击“确定”,如图

java ldle安装_java ldle安装_15


(2)CATALINA_HOME,该变量的值与TOMCAT_HOME相同,设置方法同(1),如图

java ldle安装_maven_16


(3)在“系统变量”中找到Path变量,双击打开Path变量,在“变量值”的最后面添加

  %CATALINA_HOME%\bin

  (后面没有分号),如图

java ldle安装_maven_17


如果是win10,就直接添加相应的项即可。

平时的开发环境,搭建到这一步即可

3.2.2 如果要搭建成服务

(4)在“系统变量”中找到CLASSPath变量,双击打开CLASSPath变量,在“变量值”的最后面添加

  %CATALINA_HOME%\lib\servlet-api.jar

  (后面没有分号),如图

java ldle安装_maven_18


5、单击“开始”—“运行”,键入"cmd",在控制台输入service install Tomcat9,

java ldle安装_maven_19


在系统中安装Tomcat9服务项。

  然后进入控制面板—系统和安全—管理工具—服务,找到Apache Tomcat Tomcat9服务项,右击该项,点“启动”,启动该服务,如图

java ldle安装_apache_20


6、打开浏览器,地址栏输入http://localhost:8080或 http://127.0.0.1:8080

  如果出现tomcat示例主页,则表示服务器安装成功,如图

java ldle安装_tomcat_21


如果8080端口被占用了,可以修改tomcat的访问端口,方法如下:找到D:\Tomcat\apache-tomcat-9.0.0.M1-windows-x64\apache-tomcat-9.0.0.M1\conf目录下的server.xml文件,打开该文件,找到以下内容

java ldle安装_java ldle安装_22


注:每次需要启动tomcat服务器都要运行startup.bat文件。如果觉得麻烦可以把服务里启动类型设置为自动。但这样开机速度会慢些。

安装Apache Tomcat9.0的前提是必须安装JDK,如果没有安装JDK的用户可以点击下载jdk1.8.0_77(32/64位)

3.2.3 win10配置环境变量

这个步骤网上叫教程有很多不同版本,本人尝试很多种不行后,以下是亲测有效的。

Step1:右键 “此电脑”(如果Win10桌面没有,请百度),点击"属性",点击"高级系统设置"—>“高级”—>“环境变量”。

Step2:配置jdk的环境变量(略)

Step3:在"系统变量"里新建变量名:CATALINA_BASE,变量值:D:\JavaEE\apache-tomcat-9.0.8(此处为你的解压包路径)

Step4:在"系统变量"里新建变量名:CATALINA_HOME,变量值:D:\JavaEE\apache-tomcat-9.0.8

Step5:在"系统变量"里打开Path变量,添加变量值:%CATALINA_HOME%\lib;%CATALINA_HOME%\bin(这一步可有可无,有些电脑无需配置此步骤便可完成,因此可以先跳过Step5)

示例图如下:按顺序1,2,3执行。

java ldle安装_apache_23


按图标记顺序1,2,3配置

Step6:Win+R,输入cmd,按Enter,进入命令行界面。

进入tomcat下的bin目录(本人电脑的路径为D:\JavaEE\apache-tomcat-9.0.8\bin),执行“service.bat install” 。(附:service卸载命令:service.bat remove)

3.3 测试

打开安装路径,运行 bin - startup.bat, 然后打开一个你喜欢的浏览器,输入地址 localhost:8080

, 如果能显示经典的 Tomcat 小猫,就说明安装成功了,测试完成后记得运行 shutdown.bat 结束服务器。

java ldle安装_apache_24


不要关闭这个窗口

java ldle安装_tomcat_25


java ldle安装_java ldle安装_26

3.4 tomcat简单配置

打开安装路径 - conf - server.xml,在 标签里添加 URIEncoding=UTF-8,这样可以解决 get 请求的 URI 中文乱码问题,还有,如果不想用 8080 端口,直接修改 port 为你想设的端口就可以了。

java ldle安装_java ldle安装_27

3.5 在eclipse中配置

打开 Eclipse, 点击 Window - Preferences - Server - Runtime Environments, 点击 Add…, 选择 Apache Tomcat v8.5, next, 改一个喜欢的名字,选择 Tomcat 安装路径,最后 finish。

java ldle安装_maven_28


java ldle安装_apache_29


java ldle安装_apache_30


选择主界面的 Servers 标签页,点击 …create a new server…, 给服务器改个喜欢的名字,选择上一步配置的环境,finish。

java ldle安装_tomcat_31


java ldle安装_java ldle安装_32


双击刚创建的服务器,选择 Server Locations 为 Use Tomcat installation, 并把 Deploy path 配置为 webapps,这样每次运行 Tomcat 都会把项目发布到 Tomcat 的 webapps 目录。

java ldle安装_maven_33


点击 Window - Preferences - Web - JSP Files, Encoding 一项选择 UTF-8, 这样每次新建的 JSP 页就默认都是 UTF-8 编码了。

java ldle安装_tomcat_34

4. 配置和试用maven

4.1 maven版本更新

可以用eclipse已经安装好的插件版本,也可以自己更新成相应的版本。

内嵌的maven如下,版本是3.6.1,可以使用这个版本。

java ldle安装_java ldle安装_35

4.2 仓库配置文件

settings.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->
  <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
	<mirror>
		<id>alimaven</id>
		<mirrorOf>central</mirrorOf>
		<name>aliyun maven</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
	<mirror> 
		<id>CN</id> 
		<name>OSChina Central</name> 
		<url>http://maven.oschina.net/content/groups/public/</url> 
		<mirrorOf>central</mirrorOf> 
	</mirror>
	<mirror>
		<id>repo2</id>
		<name>Mirror from Maven Repo2</name>
		<url>http://repo2.maven.org/maven2/</url>
		<mirrorOf>central</mirrorOf>
	</mirror>	
  </mirrors>

  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>

4.3 更新eclipse里的maven设置

java ldle安装_maven_36

4.4 试建helloworld的maven项目

java ldle安装_maven_37


java ldle安装_apache_38


java ldle安装_java ldle安装_39


耐心等待一下,就可以得到对应的项目结构了,如下

java ldle安装_maven_40


设置项目,排除错误

右键项目,选择properties

java ldle安装_apache_41

更新jre版本

java ldle安装_maven_42


java ldle安装_maven_43


java ldle安装_java ldle安装_44


java ldle安装_maven_45


java ldle安装_java ldle安装_46

添加Server Runtime

java ldle安装_java ldle安装_47


java ldle安装_tomcat_48


java ldle安装_apache_49


java ldle安装_maven_50

4.5 更新后的maven项目结构

java ldle安装_apache_51

4.6 查看pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.edu.cuit</groupId>
  <artifactId>j2018001001-maven01project</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>j2018001001-maven01project Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>j2018001001-maven01project</finalName>
  </build>
</project>

4.7 查看web.xml文件

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

4.8 查看index.jsp文件

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

右键,启动on Server项目

java ldle安装_tomcat_52


在浏览器中查看到结果

java ldle安装_maven_53