Elasticsearch: Set Java version without using JAVA_HOME - java

In Windows environments, is there a way to indicate ElasticSearch (installed as a service) the Java location to be used without using the JAVA_HOME environment variable?
In order to deliver ElasticSearch to our customers we would like to deliver it with a private JRE 8, so we need to point to that specific version. The easiest way is to use the JAVA_HOME environment variable. However, we would like to avoid interfering with other applications that may use that variable.

I've solved my own issue by using a ZIP version of Elasticsearch (instead of the *.msi). The idea is to set the JAVA_HOME to the private JRE/JDK in the same installing CMD process and then call:
\bin\elasticsearch-service.bat install
\bin\elasticserach-service.bat start
Previously, the elasticsearch-service.bat must be modified to add the option --Jvm "%JAVA_HOME%%JVM_DLL%" instead of --Jvm "%%JAVA_HOME%%%JVM_DLL%"

Related

Multiple java versions on Windows

I see this question has been addressed here Multiple Java versions running concurrently under Windows, I think some things have changed since this question was addressed.
I want to run multiple versions of java on Windows 10. While investigating this I see that Oracle (I think) has modified the PATH env variable on my machine.
There are 2 entries:
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
C:\ProgramData\Oracle\Java\javapath
Both of these entries are symbolic links to my default version of java , 1.10 in my case. I think the easiest way to run multiple versions is to remove both of these entries, add a JAVA_HOME env variable and add %JAVA_HOME%\bin to my PATH env variable. When I want to change java versions I just change my JAVA_HOME variable.
I'm wondering if people think this is the best way to accomplish what I want and also does anyone know why there are 2 different entries pointing to symbolic links for java.
I tend to remove all the java variables from the system and put them in the batch file used to start my java app(s)--if you do this you keep complete control across versions.
Even though java apps tend to work on newer java versions, sometimes a program will install an older java version and modify your path/env, so you should probably have your batch file put your desired java bin path at the head of the path variable and overwrite the java_home variable (I think java_opts is used as well and could be set by your batch file).
You can also have a batch file add to the path and then run "CMD" which launches a new shell. This allows you to develop or run java straight from the command line without permanently modifying your environment.
Java never has a simple command line, so I'd think anyone that had to type java -jar … even once would want to build a batch file so they could just launch it like any .exe anyway, so why not set up your environment in there as well?
On macOS, one can use jENV to manage multiple java versions. There is an alternative of this in Windows - https://github.com/FelixSelter/JEnv-for-Windows/blob/main/jenv.bat

How to install ElasticSearch without setting JAVA_HOME globally

I'm trying to install ElasticSearch on my machine. The goal is to install everything without setting JAVA_HOME as it may interfere with other software
I downloaded the latest JRE and installed it. At this stage JAVA_HOME is not set and that's exactly what I was expecting
Then, I ran the following:
elasticsearch-service.bat install elasticsearch-service
"C:\path_to_my_jre"
First problem: if the path to my JRE contains whitespaces the command will fail. If I place the JRE on a path without whitespaces it works fine.
After some struggling the ElasticSearch service is now installed correctly
Unfortunately if I try to run the service (from the Windows services panel) it doesn't start, the log file states that JAVA_HOME is not set.
It is possible to avoid setting JAVA_HOME globally? Setting it would mean interfering with other applications and I don't want to take that risk
I noticed that the Windows service being created calls the elasticsearc-service-x64.exe passing it a weird parameter \RS.... . What's that parameter? Can I pass a custom JRE path to that executable and avoid setting JAVA_HOME alltogether?

Changing between Java8 and Java7 Environments

I am trying to change between Java7 and Java 8 environments. I created the following new environment variables:
JAVA7_HOME - java 7 location
JAVA8_HOME - java 8 location
JAVA_HOME
I then set JAVA_HOME to be %JAVA8_HOME% or %JAVA7_HOME%. In path I have
%JAVA_HOME%\bin;REST OF PATH
It seems no matter what I set JAVA_HOME to it will not change the outcome of java -version, even for new sessions.
How can we have two separate java environments while easily changing between them? I've tried setting everything to the correct paths, that includes:
HKEY_CURRENT_USER\Environment JAVA_HOME
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment CurrentVersion
and even changing symlinks to point to the version we want. We've create batch scripts to do it all, we've create powershell/,net to do it and then broadcast, tried setting it all and restarting explorer.. It seems impossible to switch between 7 and 8.
Does anyone have an idea how to resolve this?
The basic problem is that changing environment variables via commandline is not permanent for some reason. You'll have to set the new value via the UI.
I used to do the following:
Put both the jdk8 and the jdk7 in your path (put the one you want as "default" first).
Look up java.exe and javac.exe in the jdk8 bin/ directory. Copy the executables under the name java8.exe and javac8.exe.
Do the same in the jdk7 bin/ directory, and copy the binaries to java7.exe and javac7.exe.
Now you can call java/javac, java7/javac7 and java8/javac8.
(I am not sure why anybody would need JAVA_HOME, I never set it.)
The other excutables like jar.exe and so on will be taken from the default jdk (the one first in the path), this is ok, since your JAR files will be the same no matter which jar.exe made them.
Then go on to configure the applications you need, like eclipse.
Hopefully, also your build tool can be told which executables to use. If not, it will use the default.
This way, you can also realize crazy configurations. For example, suppose you want jdk7 as default, but want to always use the java8 javadoc. Then simply rename the javadoc.exe from jdk7, for example to javadoc-dontuse.exe. This way, when you type javadoc.exe or some application calls it, it will not find it in the (default) jdk7 and continue to search in the jdk8 bin.

jdk installer for windows 7 does not set path

It seems the JDK installer on Windows 7 does not install a path variable to find javac. I can set it manually. But I do not like to change it with any new update of JDK. Does it use any other environment variable? Or is it really that weird?
I don't know of a JDK installer which sets the PATH on any system.
You can install several version of the JDK and if you use an IDE, you don't need to set the path. If you want to use javac manually (don't ask me why you would) you need to set the PATH to specific which version to use.
Yes You can using environment variables. Which available in Control panel -> system

Multiple Java versions running concurrently under Windows

We have a couple of applications running on Java 5 and would like now to bring in an application based on Java 6. Can both java versions live together under Windows?
Is there any control panel to set the appropriate Java version for different applications, or any other way to set up, what version of Java will be used to run that particular application?
Of course you can use multiple versions of Java under Windows. And different applications can use different Java versions. How is your application started? Usually you will have a batch file where there is something like
java ...
This will search the Java executable using the PATH variable. So if Java 5 is first on the PATH, you will have problems running a Java 6 application. You should then modify the batch file to use a certain Java version e.g. by defining a environment variable JAVA6HOME with the value C:\java\java6 (if Java 6 is installed in this directory) and change the batch file calling
%JAVA6HOME%\bin\java ...
I was appalled at the clumsiness of the CLASSPATH, JAVA_HOME, and PATH ideas, in Windows, to keep track of Java files. I got here, because of multiple JREs, and how to content with it. Without regurgitating information, from a guy much more clever than me, I would rather point to to his article on this issue, which for me, resolves it perfectly.
Article by: Ted Neward: Multiple Java Homes: Giving Java Apps Their Own JRE
With the exponential growth of Java as a server-side development language has come an equivablent
exponential growth in Java development tools, environments, frameworks, and extensions.
Unfortunately, not all of these tools play nicely together under the same Java VM installation. Some
require a Servlet 2.1-compliant environment, some require 2.2. Some only run under JDK 1.2 or above,
some under JDK 1.1 (and no higher). Some require the "com.sun.swing" packages from pre-Swing 1.0
days, others require the "javax.swing" package names.
Worse yet, this problem can be found even within the corporate enterprise, as systems developed using
Java from just six months ago may suddenly "not work" due to the installation of some Java Extension
required by a new (seemingly unrelated) application release. This can complicate deployment of Java
applications across the corporation, and lead customers to wonder precisely why, five years after the
start of the infamous "Installing-this-app-breaks-my-system" woes began with Microsoft's DLL schemes,
we still haven't progressed much beyond that. (In fact, the new .NET initiative actually seeks to solve the
infamous "DLL-Hell" problem just described.)
This paper describes how to configure a Java installation such that a given application receives its own,
private, JRE, allowing multiple Java environments to coexist without driving customers (or system
administrators) insane...
It is absolutely possible to install side-by-side several JRE/JDK versions. Moreover, you don't have to do anything special for that to happen, as Sun is creating a different folder for each (under Program Files).
There is no control panel to check which JRE works for each application. Basically, the JRE that will work would be the first in your PATH environment variable. You can change that, or the JAVA_HOME variable, or create specific cmd/bat files to launch the applications you desire, each with a different JRE in path.
We can install multiple versions of Java Development kits on the same machine using SDKMan.
Some points about SDKMan are as following:
SDKMan is free to use and it is developed by the open source community.
SDKMan is written in bash and it only requires curl and zip/unzip programs to be present on your system.
SDKMan can install around 29 Software Development Kits for the JVM such as Java, Groovy, Scala, Kotlin and Ceylon. Ant, Gradle, Grails, Maven, SBT, Spark, Spring Boot, Vert.x.
We do not need to worry about setting the _HOME and PATH environment variables because SDKMan handles it automatically.
SDKMan can run on any UNIX based platforms such as Mac OSX, Linux, Cygwin, Solaris and FreeBSD and we can install it using following commands:
$ curl -s "https://get.sdkman.io" | bash
$ source "$HOME/.sdkman/bin/sdkman-init.sh"
Because SDKMan is written in bash and only requires curl and zip/unzip to be present on your system. You can install SDKMan on windows as well either by first installing Cygwin or Git Bash for Windows environment and then running above commands.
Command sdk list java will give us a list of java versions which we can install using SDKMan.
Installing Java 8
$ sdk install java 8.0.201-oracle
Installing Java 9
$ sdk install java 9.0.4-open
Installing Java 11
$ sdk install java 11.0.2-open
Uninstalling a Java version
In case you want to uninstall any JDK version e.g., 11.0.2-open you can do that as follows:
$ sdk uninstall java 11.0.2-open
Switching current Java version
If you want to activate one version of JDK for all terminals and applications, you can use the command
sdk default java <your-java_version>
Above commands will also update the PATH and JAVA_HOME variables automatically. You can read more on my article How to Install Multiple Versions of Java on the Same Machine.
It should be possible changing setting the JAVA_HOME environment variable differently for specific applications.
When starting from the command line or from a batch script you can use set JAVA_HOME=C:\...\j2dskXXX to change the JAVA_HOME environment.
It is possible that you also need to change the PATH environment variable to use the correct java binary. To do this you can use set PATH=%JAVA_HOME%\bin;%PATH%.
I use a simple script when starting JMeter with my own java version
setlocal
set JAVA_HOME="c:\java8"
set PATH=%JAVA_HOME%\bin;%PATH%;
java -version
To have a java "portable"
you can use this method here:
https://www.whitebyte.info/programming/java/how-to-install-a-portable-jdk-in-windows-without-admin-rights
Or use links. While it is rather unpleasant to update the PATH in a running environment, it's easy to recreate a link to a new version of JRE/JDK. So:
install different versions of JDK you want to use
create a link to that folder either by junction or by built-in mklink command
set the PATH to the link
If other version of java is to be used, delete the link, create a new one, PATH/JAVA_HOME/hardcoded scripts remain untouched
Invoking Java with "java -version:1.5", etc. should run with the correct version of Java. (Obviously replace 1.5 with the version you want.)
If Java is properly installed on Windows there are paths to the vm for each version stored in the registry which it uses so you don't need to mess about with environment versions on Windows.
If you use Java Web Start (you can start applications from any URL, even the local file system) it will take care of finding the right version for your application.
Using Java Web Start, you can install multiple JRE, then call what you need.
On win, you can make a .bat file:
1- online version:
<your_JRE_version\bin\javaws.exe> -localfile -J-Djnlp.application.href=<the url of .jnlp file.jnlp> -localfile -J "<path_temp_jnlp_file_.jnlp>"
2- launch from cache:
<your_JRE_version\bin\javaws.exe> -localfile -J "<path_of_your_local_jnlp_file.jnlp>"

Categories

Resources