1

I have installed Java 11 and set up system variables as:

JAVA_HOME
C:\Program Files\AdoptOpenJDK\jdk-11.0.5.10-hotspot

and Path to

C:\Program Files\AdoptOpenJDK\jdk-11.0.5.10-hotspot\bin

When I try to install tomcat as service using service.bat, I got the below error:

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

I did research online. My paths is set up correctly. Some solution said to install java in the directory without spaces, but I am getting same error.

The echo java command is giving me the right output

echo %JAVA_HOME%
C:\Program Files\AdoptOpenJDK\jdk-11.0.5.10-hotspot

Any help is appreciated. Thanks

Edit1: To Andres answer, below is my line 57 -69

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
if not exist "%JAVA_HOME%\jre\bin\java.exe" goto noJavaHome
if not exist "%JAVA_HOME%\jre\bin\javaw.exe" goto noJavaHome
if not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome
if not "%JRE_HOME%" == "" goto okJavaHome
set "JRE_HOME=%JAVA_HOME%\jre"
goto okJavaHome
: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
Rose
  • 1,490
  • 5
  • 25
  • 56
  • `java -version` output? – Roberto Manfreda Dec 19 '19 at 01:02
  • @RobertoManfreda openjdk version "11.0.5" 2019-10-15 OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.5+10) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.5+10, mixed mode) – Rose Dec 19 '19 at 01:07
  • Make sure you don't have something like JRE_HOME in your variables. Maybe an old jdk_8 related variable. If the problem persists try to move your JAVA_HOME in system variables (if you haven't already). Otherwise your configuration seems to be correct! – Roberto Manfreda Dec 19 '19 at 01:25
  • I don’t have JRE_HOME set up. I did try moving Java Home to user variable but that didn’t work. – Rose Dec 19 '19 at 01:27
  • Try to move in system variables. `echo %PATH%` output? – Roberto Manfreda Dec 19 '19 at 01:29
  • I just reread your question. From what you wrote it looks like you've declared a variable named path. This is not correct. You need to add jthe JAVA_HOME variable to the Path variable that already exists between the system variables. Follow this guide: https://www.mkyong.com/java/how-to-set-java_home-on-windows-10/ – Roberto Manfreda Dec 19 '19 at 01:39
  • What version of Tomcat 7 are you using? --- I ask because the [change log](https://tomcat.apache.org/tomcat-7.0-doc/changelog.html) for Tomcat 7.0.83 says: *[61590](https://bz.apache.org/bugzilla/show_bug.cgi?id=61590): Enable `service.bat` to recognise when `JAVA_HOME` is configured for a Java 9 JDK.* --- The new directory structure of the JDK, which is used by Java 11, was introduced in Java 9. – Andreas Dec 19 '19 at 02:20
  • @Andreas I am using Tomcat 7.0.96. The link you shared shows that issue was fixed for 7.0.x for 7.0.83 onwards.I do see service.batch file have commands like "if not exist "%JAVA_HOME%\jre\bin\java.exe" goto noJavaHome". My java directory doesn't have JRE folder. What should I do? – Rose Dec 19 '19 at 03:03

2 Answers2

1

To re-cap the information:

  • JAVA_HOME is defined
  • JRE_HOME is not defined (from comment)
  • Tomcat is version 7.0.96 (from comment)
  • You're executing service.bat

Here is relevant snippet of service.bat from the apache-tomcat-7.0.96-windows-x64.zip file:

47  rem Make sure prerequisite environment variables are set
48  if not "%JAVA_HOME%" == "" goto gotJdkHome
 . . .
56  :gotJdkHome
57  if not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome
58  rem Java 9 has a different directory structure
59  if exist "%JAVA_HOME%\jre\bin\java.exe" goto preJava9Layout
60  if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
61  if not "%JRE_HOME%" == "" goto okJavaHome
62  set "JRE_HOME=%JAVA_HOME%"
63  goto okJavaHome
 . . .
73  :okJavaHome

The batch script executes as follows:

  • 48: JAVA_HOME is defined, jump to line 56
  • 57: %JAVA_HOME%\bin\javac.exe exists, go to next line
  • 59: %JAVA_HOME%\jre\bin\java.exe does not exist, go to next line
  • 60: %JAVA_HOME%\bin\java.exe exists, go to next line
  • 61: JRE_HOME is undefined, go to next line
  • 62: set JRE_HOME to same value as JAVA_HOME
  • 63: jump to line 73

Now, if you get the message you say, which is printed by lines 69-71, then one of those checks failed, so check them out manually using the following commands:

Command                                 Expected output
==================================      ===============
echo %JAVA_HOME%                        C:\Program Files\AdoptOpenJDK\jdk-11.0.5.10-hotspot
dir /b "%JAVA_HOME%\bin\javac.exe"      javac.exe
dir /b "%JAVA_HOME%\jre\bin\java.exe"   The system cannot find the path specified.
dir /b "%JAVA_HOME%\bin\java.exe"       java.exe
echo %JRE_HOME%                         %JRE_HOME%

When you find which one doesn't give the expected output, you'll know why you get the message you get.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • and make sure you execute those commands as the same user that's going to be running the service else you might run into problems with access restrictions. – jwenting Dec 19 '19 at 06:00
  • @jwenting The service is actually configured to run as `Local System` by default, so that comment is not applicable. – Andreas Dec 19 '19 at 06:03
  • Hi @Andreas I tried all your commands and they are giving me the expected output. I have also pasted the section of my service.bat in my question – Rose Dec 19 '19 at 17:37
  • 1
    @Rose That's really weird, because I got the `service.bat` file in my answer from the `apache-tomcat-7.0.96-windows-x64.zip` file at `https://archive.apache.org/dist/tomcat/tomcat-7/v7.0.96/bin/`, and you [said](https://stackoverflow.com/questions/59402002/installing-tomcat-7-as-a-service-on-java-11nb-java-home-should-point-to-a-jdk?noredirect=1#comment104994305_59402002) you are using Tomcat 7.0.96, so how can yours be different? Please recheck which version of Tomcat 7 you have. – Andreas Dec 19 '19 at 19:52
0

I had to install JRE 11 on my laptop and paste it inside the JDK folder. And then I was able to install tomcat as a service with the same path and JAVA_HOME as mentioned in my question.

Rose
  • 1,490
  • 5
  • 25
  • 56