小编也是被网上一堆配置JDK环境变量给坑了,环境变量干嘛的?我的理解就是给系统一个路径在Dos窗口执行命令时让系统知道该去什么地方搜索对应的命令文件。具体查看百度“环境变量”
我说说我的环境变量配置如下我安装的jdk9.0.1在jdk目录下没有jre目录而是分开在同一个目录下的,我的JDK,JRE安装目录都是默认的文件位子如下:
我在系统环境变量中分别配置了JAVA_HOME和JRE_HOME
JAVA_HOME C:\Program Files\Java\jdk-9.0.1 //jdk的目录
JRE_HOME C:\Program Files\Java\jre-9.0.1 //jre的目录
CLASSPATH .;%JAVA_HOME%\lib //可执行包所在目录
path中增加两条 %JAVA_HOME%\bin; 和 %JRE_HOME%\bin; //可执行文件搜索目录
读者可以自己查询百度了解JAVA_HOME、JRE_HOME、CLASSPATH、path的具体作用。
之后我们回到Tomcat自启动配置我们在cmd命令行执行Service.bat
(1)在cmd中进入Tomcat目录bin目录,执行service.bat install Tomcat
很多人在此处会遇到错误如下:
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
我们打开service.bat查看,修改以下特殊标注几处“这网站有毒标红几处变成了web的这个玩意儿,我和客服反映过几次了没什么卵用~~~~”
@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License. You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.
rem ---------------------------------------------------------------------------
rem NT Service Install/Uninstall script
rem
rem Options
rem install Install the service using Tomcat9 as service name.
rem Service is installed using default settings.
rem remove Remove the service from the System.
rem
rem name (optional) If the second argument is present it is considered
rem to be new service name
rem ---------------------------------------------------------------------------
rem set JRE_HOME=C:\Program Files\Java\jre-9.0.1 //可选修改,如果没有在环境变量添加JRE_HOME可在此处设置,同时还有设置setclasspath.bat里面
//做相应的修改,去掉修改前的"rem"注释的意思
setlocal
set "SELF=%~dp0%service.bat"
rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%cd%"
if exist "%CATALINA_HOME%\bin\tomcat9.exe" goto okHome
rem CD to the upper dir
cd ..
set "CATALINA_HOME=%cd%"
:gotHome
if exist "%CATALINA_HOME%\bin\tomcat9.exe" goto okHome
echo The tomcat9.exe was not found...
echo The CATALINA_HOME environment variable is not defined correctly.
echo This environment variable is needed to run this program
goto end
:okHome
rem Make sure prerequisite environment variables are set
if not "%JAVA_HOME%" == "" goto gotJdkHome
if not "%JRE_HOME%" == "" goto gotJreHome
echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
echo Service will try to guess them from the registry.
goto okJavaHome
:gotJreHome
if not exist "%JRE_HOME%\bin\java.exe" goto noJavaHome //检查此处是否正确
if not exist "%JRE_HOME%\bin\javaw.exe" goto noJavaHome //检查此处是否正确
goto okJavaHome
:gotJdkHome
rem if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome //检查此处是否正确
rem if not exist "%JAVA_HOME%\bin\javaw.exe" goto noJavaHome //检查此处是否正确
rem if not exist "%JAVA_HOME%\javac.exe" goto noJavaHome //检查此处是否正确
if not "%JRE_HOME%" == "" goto okJavaHome
set "JRE_HOME=%JRE_HOME%"
goto okJavaHom
:noJavaHome
echo The JAVA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
echo NB: JAVA_HOME should point to a JDK not a JRE
goto end
:okJavaHome
if not "%CATALINA_BASE%" == "" goto gotBase
set "CATALINA_BASE=%CATALINA_HOME%"
:gotBase
set "EXECUTABLE=%CATALINA_HOME%\bin\tomcat9.exe"
rem Set default Service name
set SERVICE_NAME=Tomcat9
set DISPLAYNAME=Apache Tomcat 9.0 %SERVICE_NAME%
if "x%1x" == "xx" goto displayUsage
set SERVICE_CMD=%1
shift
if "x%1x" == "xx" goto checkServiceCmd
:checkUser
if "x%1x" == "x/userx" goto runAsUser
if "x%1x" == "x--userx" goto runAsUser
set SERVICE_NAME=%1
set DISPLAYNAME=Apache Tomcat 9.0 %1
shift
if "x%1x" == "xx" goto checkServiceCmd
goto checkUser
:runAsUser
shift
if "x%1x" == "xx" goto displayUsage
set SERVICE_USER=%1
shift
runas /env /savecred /user:%SERVICE_USER% "%COMSPEC% /K \"%SELF%\" %SERVICE_CMD% %SERVICE_NAME%"
goto end
:checkServiceCmd
if /i %SERVICE_CMD% == install goto doInstall
if /i %SERVICE_CMD% == remove goto doRemove
if /i %SERVICE_CMD% == uninstall goto doRemove
echo Unknown parameter "%SERVICE_CMD%"
:displayUsage
echo.
echo Usage: service.bat install/remove [service_name] [/user username]
goto end
:doRemove
rem Remove the service
echo Removing the service '%SERVICE_NAME%' ...
echo Using CATALINA_BASE: "%CATALINA_BASE%"
"%EXECUTABLE%" //DS//%SERVICE_NAME% ^
--LogPath "%CATALINA_BASE%\logs"
if not errorlevel 1 goto removed
echo Failed removing '%SERVICE_NAME%' service
goto end
:removed
echo The service '%SERVICE_NAME%' has been removed
goto end
:doInstall
rem Install the service
echo Installing the service '%SERVICE_NAME%' ...
echo Using CATALINA_HOME: "%CATALINA_HOME%"
echo Using CATALINA_BASE: "%CATALINA_BASE%"
echo Using JAVA_HOME: "%JAVA_HOME%"
echo Using JRE_HOME: "%JRE_HOME%"
rem Try to use the server jvm
set "JVM=%JRE_HOME%\bin\server\jvm.dll"
if exist "%JVM%" goto foundJvm
rem Try to use the client jvm
set "JVM=%JRE_HOME%\bin\client\jvm.dll"
if exist "%JVM%" goto foundJvm
echo Warning: Neither 'server' nor 'client' jvm.dll was found at JRE_HOME.
set JVM=auto
:foundJvm
echo Using JVM: "%JVM%"
set "CLASSPATH=%CATALINA_HOME%\bin\bootstrap.jar;%CATALINA_BASE%\bin\tomcat-juli.jar"
if not "%CATALINA_HOME%" == "%CATALINA_BASE%" set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar"
if "%SERVICE_STARTUP_MODE%" == "" set SERVICE_STARTUP_MODE=manual
if "%JvmMs%" == "" set JvmMs=128
if "%JvmMx%" == "" set JvmMx=256
"%EXECUTABLE%" //IS//%SERVICE_NAME% ^
--Description "Apache Tomcat 9.0.1 Server - http://tomcat.apache.org/" ^
--DisplayName "%DISPLAYNAME%" ^
--Install "%EXECUTABLE%" ^
--LogPath "%CATALINA_BASE%\logs" ^
--StdOutput auto ^
--StdError auto ^
--Classpath "%CLASSPATH%" ^
--Jvm "%JVM%" ^
--StartMode jvm ^
--StopMode jvm ^
--StartPath "%CATALINA_HOME%" ^
--StopPath "%CATALINA_HOME%" ^
--StartClass org.apache.catalina.startup.Bootstrap ^
--StopClass org.apache.catalina.startup.Bootstrap ^
--StartParams start ^
--StopParams stop ^
--JvmOptions "-Dcatalina.home=%CATALINA_HOME%;-Dcatalina.base=%CATALINA_BASE%;-Djava.io.tmpdir=%CATALINA_BASE%\temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties;%JvmArgs%" ^
--Startup "%SERVICE_STARTUP_MODE%" ^
--JvmMs "%JvmMs%" ^
--JvmMx "%JvmMx%"
if not errorlevel 1 goto installed
echo Failed installing '%SERVICE_NAME%' service
goto end
:installed
echo The service '%SERVICE_NAME%' has been installed.
:end
cd "%CURRENT_DIR%"
修改完成之后,在cmd执行安装service.bat命令之后,进入任务管理器,将其设置为开机自启。
教程有不完善之处希望大家指正。