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?

ahnbizcad
  • 10,491
  • 9
  • 59
  • 85

10 Answers10

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.
Community
  • 1
  • 1
prayagupa
  • 30,204
  • 14
  • 155
  • 192
  • 1
    Which means your JAVA_HOME is not set. – prayagupa Aug 30 '14 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 Aug 30 '14 at 13:53
  • How do I set it? I followed the instructions in the OP link, so I thought I set it. perhaps I typed in the wrong string? I uninstalled everything now, only did 64bit JRE, and set as a system variable `JAVA_HOME` and `C:\Progra~1\Java\jre1.8.0_20` I'm not sure how else to set it. – ahnbizcad Aug 30 '14 at 14:00
  • 1
    @gwho Please follow *STEP 2* link provided in my answer. Hope that helps – prayagupa Aug 30 '14 at 14:04
  • I followed it. I set the variable value to `C:\Program Files\Java\jre1.8.0_20`. I then went to the terminal, did `set JAVA_HOME = C:\Program Files\Java\jre1.8.0_20`. It just took it, and presented a new line. I then tried `echo %JAVA_HOME%`, and it still gives me the exact string `%JAVA_HOME%` – ahnbizcad Aug 30 '14 at 14:13
  • 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. – prayagupa Aug 30 '14 at 14:24
  • @gwho Also I receommend JDK not JRE. – prayagupa Aug 30 '14 at 14:24
  • I see your new summary. I followed those instructions exactly. I even restarted my terminal. Would restarting my computer be worth a shot? I'll uninstall JRE, and install JDK instead, and restart my computer. – ahnbizcad Aug 30 '14 at 14:28
  • 1
    Maybe. Do you see `jdk` in list once you do `echo %PATH%` or `echo $Env:PATH`. If not give it a restart. – prayagupa Aug 30 '14 at 14:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60297/discussion-between-gwho-and-prayag-upd). – ahnbizcad Aug 30 '14 at 14:46
  • 1
    I can't see how the chat developed; **I needed a restart.** In my case, I defined JAVA_HOME, and verified as described in Step 1. And I could run `java -version` as described in Step 1. And yet, when I ran the `.bat`, the ` if DEFINED JAVA_HOME` did not work. *Why would cmd.exe session find DEFINED JAVA_HOME, but a batch file could not?* – Nate Anderson Feb 28 '16 at 05:29
  • On Windows if you're using the bundled service batch files, you may also need to "remove" and "install" the Windows Service via `service.bat remove` and `service.bat install` since the JAVA_HOME is read at service install. – Adam Caviness Jun 23 '16 at 13:56
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

Community
  • 1
  • 1
BatScream
  • 19,260
  • 4
  • 52
  • 68
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.

fgul
  • 5,763
  • 2
  • 46
  • 32
  • thank you! this is the correct answer if java_home is already set. – Ohad Jun 08 '20 at 08: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 Apr 08 '22 at 04:01
3

run the below command:

set JAVA_HOME=C:\Program Files\Java\jre8
VISHNU
  • 948
  • 8
  • 15
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

Vijay Sutaria
  • 132
  • 1
  • 11
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.

breght
  • 166
  • 1
  • 8
  • 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 Aug 30 '14 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.

mohan08p
  • 5,002
  • 1
  • 28
  • 36
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.

Ivan Yurchenko
  • 3,762
  • 1
  • 21
  • 35
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.

Rodolfo Luna
  • 829
  • 9
  • 19