24

I try to set the JAVA_HOME path as my Tomcat server is looking for it. I am trying to set it but it doesn't seem to work and causes an error when I do. I am trying to set the JAVA in the setclasspath.bat using

set JAVA_HOME="C:\Program Files (x86)\Java\jre7"

This is at the start of the setclasspath.bat

set JAVA_HOME="C:\Program Files (x86)\Java\jre7"
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 At least one of these environment variable is needed to run this program
goto exit

When I set this and run "startup.bat start" it displays

Files was unexpected at this time

Can you help me?

2
  • Try setting JAVA_HOME as system environment variable, go to Advanced Tab of System settings, click on New and set JAVA_HOME. Aug 27, 2013 at 14:49
  • 3
    Your path contains whitespaces, this isn't working in .BAT files. You have to enclose your path in quotation marks. set JAVA_HOME="C:\Program Files (x86)\Java\jre7". However you are pointing to a JRE, for tomcat to work you have to point to a JDK (tomcat needs a compiler and that isn't part of the JRE but of the JDK).
    – M. Deinum
    Aug 27, 2013 at 15:08

9 Answers 9

46

Note: I realise this is already quite an old question, but many of the answers posted here are either incomplete or inaccurate.. Hopefully this will help save a few headaches.

Firstly: Tomcat does not need a JDK to run, it will work just fine with a JRE, as long as it knows it's a JRE.

Secondly, the error from the original question is coming from an issue with syntax of the set JAVA_HOME=... command. Apache themselves could handle it better with stripping and adding " quote marks.

Also, I would highly recommend creating a setenv.bat file in the bin folder. It's absent by default, so if you don't already have one, create it and add your set JAVA_HOME=... or set JRE_HOME=... lines there.

Run with JRE

As per running.txt:

The JRE_HOME variable is used to specify location of a JRE. The JAVA_HOME variable is used to specify location of a JDK.

Using JAVA_HOME provides access to certain additional startup options that are not allowed when JRE_HOME is used.

If both JRE_HOME and JAVA_HOME are specified, JRE_HOME is used.

So, to startup this way, you'll need the following:

set "JAVA_HOME="
set "JRE_HOME=C:\Program Files (x86)\Java\jre7"

Clearing the JAVA_HOME variable is a failsafe, but it's not really required. As per the docs, Tomcat will try use the JRE variable first anyway.

Solution for Issue in Question

Take special note the position of the quotation marks. This way keep the entire string together in one variable, without including the quotation marks in the variable content itself.

For example:

set %TEST%="hello"
echo "%TEST%"

Will output ""hello"".

set "%TEST%=hello"
echo "%TEST%"

Will output "hello".

So, the startup batch file script was trying to use ""C:\Program Files (x86)\Java\jre7"", in which the first non-escaped space is between "Program" and "Files".

As already pointed out, removing the quotation marks (in this particular case at least) would work, but it's dangerous to rely on that. Rather play it safe from the start and wrap the variable name and value in quotation marks.

2
  • 2
    This worked perfectly! I had initially set %JAVA_HOME%="C:\Program Files\path\to\jdk" as per this tutorial. But I cleared JAVA_HOME with set "JAVA_HOME=" and ran set "JRE_HOME=C:\Program Files\path\to\jre". Coming from *nix, this was beginning to be a pain. Thanks!
    – tmthyjames
    May 8, 2015 at 16:20
  • I just set a permanent environment variable JRE_HOME = C:\.....\jdk. Working fine now. Now there is no need to write that setenv.bat file right??
    – Sriman S
    Aug 3, 2021 at 9:53
10

I had the same problem on Windows 7 with the following definition (I put it in a setenv.bat file in the jdk bin folder as specified by tomcat 7's RUNNING.txt.

set JAVA_HOME="C:\Program Files (x86)\Java\jre7"

I just tried removing the double quotes altogether:

set JAVA_HOME=C:\Program Files (x86)\Java\jre7

And tomcat then started.

0
7
  • First, install java and locate the instalation path.
  • Locate the tomcat installation path and find the startup.bat in \bin
  • Open startup.bat and add below(example) lines just after setlocal and before rem Guess CATALINA_HOME if not defined
:: JAVA
set JAVA_HOME=D:\thushara_data\Java\jdk1.8.0_73
set PATH=%JAVA_HOME%\bin;%PATH%

Now try to re-run startup.bat

2

Place the path in quotes:

set JAVA_HOME="C:\Program Files (x86)\Java\jre7"

The error is due to the fact that it's parsing the Files in Program Files as a separate parameter, which SET doesn't expect. Why SET isn't reading it properly I can't say without knowing more about what OS you're using, what command shell you're running Tomcat from, and so on.

1
  • I am running it on Windows Server 2008 R2 Enterprise 64bi and the code about is a bat file; I am just running Tomcat through CMD.
    – QuinsUK
    Aug 27, 2013 at 16:09
2
  • If not able to set the variables manually: to set JAVA_HOME and JRE_HOME

  • go to advance system setting and click on environment variables

  • under system variables click

  • new variable name : JAVA_HOME
  • variable value : [path of jdk]C:\Program Files\Java\jdk1.8.0_181 and click ok

  • similarly add JRE_HOME

  • click new variable name : JRE_HOME
  • variable value : [path of jre]C:\Program Files\Java\jre1.8.0_181
  • click Ok > Ok > Ok
1

You are pointing to a JRE. You need to point to a JDK, which given the location of your JRE might be something like C:\Program Files (x86)\Java\jdk1.6.0_26 or similar. Or, if you only have a JRE installed, you'll need to install a full JDK. Tomcat needs to be able to compile JSPs into .class files, which a JRE can't do.

1
  • 4
    Downvoted because this answer is blatantly incorrect. I've outlined what causes the initial problem and how to avoid it in my answer here.
    – user909694
    Mar 16, 2015 at 12:24
1

The reason why your command failed is because it has white spaces between 'Program Files (x86)' and so it starts reading the command from 'Files (x86)'. So instead we put it in between double quotes such as the following. try this..

set JAVA_HOME="C:\Program Files (x86)\Java\jre7"
0
0

Try https://askubuntu.com/questions/446294/how-to-start-tomcat7-when-catalina-sh-does-not-work It can give you some insight even though it's not Windows it still looks for those shell scripts except their extensions in Window is .bat not .sh

0

You Can Also Try This For jre1.8v:

Open cmd In Tomcat Server Folder And Enter Command

set "JAVA_HOME=C:\Program Files (x86)\Java\jre1.8.0_45"

Then Enter Command cd bin

Then To Start Tomcat Server Enter Command startup

To Stop Tomcat Server Enter Command shutdown

If It Fails To Start Although You Can Run Command: cd startup.bat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.