Neither the JAVA_HOME nor the JRE_HOME environment variable is defined ;At least one of these environment variable is needed to run this program

时间:2020-12-05 00:24:58

linux下,关闭和重启tomcat时报错:

Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program

我的tomcat以前是正常的,突然重启下tomcat时报了这个错,错误大致是环境变量不可用造成的,

在网上找了很久都是说要修改setclasspath.bat(该文件路径在tomcat的bin目录下)文件;在文件中加入JAVA_HOME和JRE_HOME的环境变量,

我也在该文件中加了如下:

rem Make sure prerequisite environment variables are set

rem In debug mode we need a real JDK (JAVA_HOME)

export JAVA_HOME=/usr/local/jdk1.7.0_79
export JRE_HOME=${JAVA_HOME}/jre

if ""%1"" == ""debug"" goto needJavaHome

rem Otherwise either JRE or JDK are fine
if not "%JRE_HOME%" == "" goto gotJreHome
if not "%JAVA_HOME%" == "" goto gotJavaHome
echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
echo At least one of these environment variable is needed to run this program
goto exit

:needJavaHome
rem Check if we have a usable JDK
if "%JAVA_HOME%" == "" goto noJavaHome
if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
if not exist "%JAVA_HOME%\bin\javaw.exe" goto noJavaHome
if not exist "%JAVA_HOME%\bin\jdb.exe" goto noJavaHome
"setclasspath.bat" [dos] 97L, 3509C

保存好文件,再次运行./startup.sh 

仍然报错:

Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program

崩溃了,网上就这么一种解决问题的方法;

突然我意识上闪出一个念头,前面使用tomcat也是正常的,会不会在linux在运行的内存里面丢失了JAVA_HOME和JRE_HOME

的环境变量参数,如果是那样,就重新写入/etc/profile环境变量就ok了。

接着我运行了#source /etc/profile

重新写入profile配置文件

运行#java -version 

奇迹出现了

java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

切换到tomcat的bin目录下

运行#./startup.sh

tomcat运行成功!