37

For Windows 7, I've installed JDK in both 32 and 64 bit versions. The versions are 8, update 20 for all four installations. http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

when I download and unzip elasticsearch, and run /bin/service.bat or /bin/elasticsearch.bat files, I get in my terminal:

JAVA_HOME environment variable must be set! Press any key to continue . . .

When I do, the terminal closes.

I've uninstalled and reinstalled, but that didn't fix it. Why wouldn't the environment varaible get set? Is it an installation thing, or do I actually have to manually set the variable. It just seems like if it installed correctly, it would set the variable automatically... Please help. Thanks

I've manually set the JAVA_HOME environment variable using this.

https://confluence.atlassian.com/display/DOC/Setting+the+JAVA_HOME+Variable+in+Windows

My path is C:\Progra~1\Java\jdk1.8.0_20\jre

Now, when I run service.bat, the terminal immediately closes upon opening. When I run elasticsearch.bat, the terminal prints some lines, and after a while, it just stays there, and I can't type anything.


EDIT:

I uninstalled all java, reinstalled only the JRE 64 bit, and set the system environment variable to JAVA_HOME and `C:\Progra~1\Java\jre1.8.0_20

I have discovered that there is CLASSPATH that's set to a 32bit, older java version. Should I delete that variable?

2
  • I'm going to try uninstalling, and reinstalling the JRE instead of the JDK.
    – ahnbizcad
    Commented Aug 30, 2014 at 13:34
  • 1
    If you mean path to 32 bit java is set in classpath, you can remove that part only from classpath. Commented Aug 30, 2014 at 14:14

10 Answers 10

59

STEP 1

Can you verify JAVA_HOME is set to C:\Progra~1\Java\jdk1.8.0_20,

C:\>echo %JAVA_HOME%

Also, verify java command is available,

 java -version

Reference

Check if JAVA_HOME is present in environment using batch script

STEP 2

If JAVA_HOME is not set, please follow the steps provided here - How to Set Java Home in windows

Summary ,

  • Right-click the My Computer icon on your desktop and select Properties.
  • Click the Advanced tab. Click the Environment Variables button. Under System Variables, click New.
  • Enter the variable name as JAVA_HOME.
  • Enter the variable value as the installation path for the JDK.

    (eg. C:\Progra~1\Java\jdk1.8.0_20).

  • Click OK.
  • Click Apply Changes.
12
  • 1
    Which means your JAVA_HOME is not set. Commented Aug 30, 2014 at 13:41
  • 1
    doing echo %JAVA_HOME% just gives me the exact string %JAVA_HOME%. But doing java-version prints out my java version.
    – ahnbizcad
    Commented Aug 30, 2014 at 13:53
  • 1
    @gwho Please follow STEP 2 link provided in my answer. Hope that helps Commented Aug 30, 2014 at 14:04
  • 2
    @gwho Still your JAVA_HOME is not set. I'm not sure but why is there spaces before and after = in set JAVA_HOME = C:\Program Files\Java\jre1.8.0_20. Try removing that. Commented Aug 30, 2014 at 14:24
  • 1
    Maybe. Do you see jdk in list once you do echo %PATH% or echo $Env:PATH. If not give it a restart. Commented Aug 30, 2014 at 14:33
4

A workaround if you do not have privileges to set up the environmental variables:

open the elasticsearch.bat file,

a) Remove the line:

if NOT DEFINED JAVA_HOME goto err

b) Replace %JAVA_HOME% with your java jdk path, something like: C:\Program Files\Java\jdk1.7.0_51

And for your terminal getting started and you not being able to type anything, it is the elasticsearch server, you need to connect to it using a client like cygwin.

https://cygwin.com/install.html

For connecting to the server refer the elasticsearch documentation.

http://www.elasticsearch.org/guide/

A similar question has already been answered:

"JAVA_HOME points to an invalid Java installation issue" with service install command in elastic search

0
4

If elasticsearch version is 7.5 or higher, you find elasticsearch-7.X.Y\bin directory and then edit the elasticsearch-env.bat as the following:

   if defined JAVA_HOME (
  set JAVA="%WRITE_ELASTICSEARCH_JAVA_PATH%\bin\java.exe" //write java custom path. Edit only this line
  set JAVA_TYPE=JAVA_HOME
) else (
  set JAVA="%ES_HOME%\jdk\bin\java.exe"
  set JAVA_HOME="%ES_HOME%\jdk"
  set JAVA_TYPE=bundled jdk
)

You can customize the java path like that for elasticsearch.

2
  • thank you! this is the correct answer if java_home is already set.
    – Ohad
    Commented Jun 8, 2020 at 8:00
  • This doesn't work for me. I've edited this and then try and run ES from services and it starts, and stops about 5 seconds later. If I update the JAVA_HOME variable then it does run.
    – Nick M
    Commented Apr 8, 2022 at 4:01
3

run the below command:

set JAVA_HOME=C:\Program Files\Java\jre8
0
3

I was facing similar issue where Java home is already set but still it was throwing error. Open elasticsearch-plugin.bat file and change "IF DEFINED JAVA_HOME" to "IF DEFINED %JAVA_HOME%" , if you have java home already set.

Thanks

1
  • I also had to do this change into elasticsearch.bat. Then I got it running. Commented Jul 28, 2017 at 6:50
1

You can set the system-wide environment variables by clicking Start, pointing to 'Computer', right-clicking and selecting properties. Next, click 'Advanced System Settings', hitting the 'Advanced' tab and selecting the 'Environment Variables' button. This brings up a window where you can set your JAVA_HOME variable. You can do this either for the current user (top box) or system-wide.

Environment variables dialog

After doing this, open a new cmd window (Start menu, type 'cmd' and hit enter) and verify JAVA_HOME is set like in the question above.

1
  • 1
    I actually did do this according to the instructions linked in the OP. I tested according to Prayad Upd's recommendation, and it failed his tests
    – ahnbizcad
    Commented Aug 30, 2014 at 13:16
1

This is pretty common error, JAVA_HOME environment is not set. So, we need to set it up in a more prominent basis. What it's asking for the path to the jre directory and not to the bin directory i.e. java environment path to configure & run the elasticsearch. So, make sure you don't specify the bin directory into your path. Then, you are good to go run your elasticsearch and it will be up and running at default port 9200.

1
  • Nice explanation! an example will be good @mohan08p Commented Oct 5, 2020 at 3:20
0

I still couldn't run it even after I set the %JAVA_HOME% environment variable. The problem for me was that I should have ran the elasticsearch.bat file as an administrator. That fixed the issue.

0

See , you just need to do below: (don't delete any environment variable already set)

  • Step 1: Edit elasticsearch-service.bat located in \bin
  • Step 2: Add below line at the beginning (below @echo) SET JAVA_HOME=C:\Program Files\Java\jdk1.8.0_162 (or your Java path)
  • Step 3: locate %%JAVA_HOME%% in the same file and replace it with %JAVA_HOME%
  • Step 4: then from cmd in bin folder run: elasticsearch-service.bat install
  • Step 5: That's it, simply run elasticsearch-service now from cmd and it will run.
0

I get this error because I was using Powershell and it wasn't seeing environment variable. (I don't know way) Then I used CMD and it ran successfully.

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