sudo update-alternatives --config java
Configures the default for the program "java". That's the Java VM.
sudo update-alternatives --config javac
Configures the default Java compiler.
You can also see that, because the first command lists a lot of "JRE" (Java Runtime Environment) folders and the Program is just called "java".
If I check which version is being used by issuing the command
java -version
or
javac -version,
I can see, that each command changes the program being used.
However, using update-java-alternatives with a JDK Version changes both programs for me. Using the first commands, you can use a Java VM and Java Compiler from different JDKs.
update-java-alternatives requires presence of a file with extension .jinfo in directory /usr/lib/jvm. The openjdk package is shipped with a .jinfo file, the jdk of Oracle (formerly Sun) is not. As alternative, you configure alternatives without update-java-alternatives:
For example, to add java from jvm-directory /usr/lib/jvm/jdk-12.0.1 (default directory of Debian package of Oracle) with priority 2082, use the following command:
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-12.0.1/bin/java 2082
As for switching for different development environments:
Are you talking about starting the IDE itself with different Java versions or using different versions in the IDE for compilation and running your app?
For 1.: You can specify which JVM to use in the eclipse.ini, as described here. I don't know how to do that for the Arduino IDE.
For 2.: In Eclipse you can select the JRE/JDK to be used in Window -> Preferences -> Java -> Installed JREs. And under Java -> Compiler you could choose an older Java compliance if you wish.
EDIT: This DigitalOcean page also has a very nice explanation of everything related to Java on Ubuntu.
Answer from Benjamin Maurer on askubuntu.comsudo update-alternatives --config java
Configures the default for the program "java". That's the Java VM.
sudo update-alternatives --config javac
Configures the default Java compiler.
You can also see that, because the first command lists a lot of "JRE" (Java Runtime Environment) folders and the Program is just called "java".
If I check which version is being used by issuing the command
java -version
or
javac -version,
I can see, that each command changes the program being used.
However, using update-java-alternatives with a JDK Version changes both programs for me. Using the first commands, you can use a Java VM and Java Compiler from different JDKs.
update-java-alternatives requires presence of a file with extension .jinfo in directory /usr/lib/jvm. The openjdk package is shipped with a .jinfo file, the jdk of Oracle (formerly Sun) is not. As alternative, you configure alternatives without update-java-alternatives:
For example, to add java from jvm-directory /usr/lib/jvm/jdk-12.0.1 (default directory of Debian package of Oracle) with priority 2082, use the following command:
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-12.0.1/bin/java 2082
As for switching for different development environments:
Are you talking about starting the IDE itself with different Java versions or using different versions in the IDE for compilation and running your app?
For 1.: You can specify which JVM to use in the eclipse.ini, as described here. I don't know how to do that for the Arduino IDE.
For 2.: In Eclipse you can select the JRE/JDK to be used in Window -> Preferences -> Java -> Installed JREs. And under Java -> Compiler you could choose an older Java compliance if you wish.
EDIT: This DigitalOcean page also has a very nice explanation of everything related to Java on Ubuntu.
update-java-alternatives is a program to update alternatives for jre/jdk installations.
update-alternatives is a symbolic link management system for linux (I'm sure there is little news here).
You can, and really should, use both update-java-alternatives and update-alternatives together.
Firstly, be sure to have the all the alternatives configured correctly. java and javac are but a few. There is javadoc, rmic, serialver and others, substituting the above variables for: native2ascii and /opt/jdk1.8.0_40/bin/native2ascii should report if the alternative is installed and/or selected.
When all the alternatives are configured you can then create links in /usr/lib/jvm to your manual instalation.
In order to configure update-java-alternatives you must use a hidden file with the same name as your directory but prefixed by a . (dot).
Hope this helps.
Bibliography
man -S 8 update-java-alternatives
http://tech.lanesnotes.com/2008/03/using-alternatives-in-linux-to-use.html
https://stackoverflow.com/questions/6477415/how-to-set-oracles-java-as-the-default-java-in-ubuntu
Replacement for Java?
java - Switch JDK version in Windows 10 cmd - Stack Overflow
I installed the latest version of java, but when I type "java --version" on terminal it shows the old one.
how can I change the default Java version?
Videos
Assuming one has installed a JDK in /opt/java/jdk1.8.0_144 then:
Install the alternative for javac
$ sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk1.8.0_144/bin/javac 1Check / update the alternatives config:
$ sudo update-alternatives --config javac
If there is only a single alternative for javac you will get a message saying so, otherwise select the option for the new JDK.
To check everything is setup correctly then:
$ which javac
/usr/bin/javac
$ ls -l /usr/bin/javac
lrwxrwxrwx 1 root root 23 Sep 4 17:10 /usr/bin/javac -> /etc/alternatives/javac
$ ls -l /etc/alternatives/javac
lrwxrwxrwx 1 root root 32 Sep 4 17:10 /etc/alternatives/javac -> /opt/java/jdk1.8.0_144/bin/javac
And finally
$ javac -version
javac 1.8.0_144
Repeat for java, keytool, jar, etc as needed.
You will notice a big change when selecting options if you type in "java -version" after doing so. So if you run update-alternatives --config java and select option 3, you will be using the Sun implementation.
Also, with regards to auto vs manual mode, making a selection should take it out of auto mode per this page stating:
When using the
--configoption, alternatives will list all of the choices for the link group of which given name is the master link. You will then be prompted for which of the choices to use for the link group. Once you make a change, the link group will no longer be inauto mode. You will need to use the--autooption in order to return to the automatic state.
And I believe auto mode is set when you install the first/only JRE/JDK.
java -version is running the wrong version of java.
Diagnostics:
>java -version
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b18)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)
the following is the Java related contents from the output of
PATH:
PATH=C:\ProgramData\Oracle\Java\javapath; ... C:\Program Files\Java\jdk1.6.0_45\bin
Conclusion:
From the above output we can deduce that C:\ProgramData\Oracle\Java\javapath is 1.8.0_66.
You need to change your PATH to put C:\Program Files\Java\jdk1.6.0_45\bin first.
I noticed that after checking the path per your suggestion. Windows 10 does not allow me to edit the path because it says "This environment variable is too large." I know there should be another question to deal with this separately.
You also need to clean up your path. My guess is you have a lot of duplicate entries.
I have the same problem, I have set JAVA_HOME:
C:\Program Files\Java\jdk1.7.0_75
and Path to:
%JAVA_HOME%\bin
I need run jdk 7. When I run java -version it always appear jdk 8.
I solved it with: in System Environment --> Path --> order %JAVA_HOME%\bin to first.
With the changes to the commercial usage for Java coming up, what are people looking at to use as a replacement that does not need commercial licensing?
I was looking at Zulu, but some of the older stations are using a A/S400 based applivation that does not seem to play well with it. Any suggestions?
Here's my guide for Windows 10.
Step 1. Go to System Properties. Click on Environment Variables
Step 2. Add new variables, such as JAVA_8_HOME
JAVA_8_HOME:%ProgramFiles%\Java\jdk1.8.0_152\binJAVA_9_HOME:%ProgramFiles%\Java\jdk-9.0.1\binJAVA_HOME:%JAVA_8_HOME%
In my case, JAVA_8_HOME(JDK8) is pointing to C:\Program Files\Java\jdk1.8.0_152\bin. You can replace this with your own path to javac. %JAVA_HOME% has a default value pointing to JAVA_8_HOME, which is the path for JDK8. That's my preference, feel free to adjust accordingly.
Step 3. Select PATH and click on Edit. PATH
Step 4. Click on New and add %JAVA_HOME%. %JAVA_HOME% will be added to PATH automatically every time you launch a command prompt.
In order to switch JDK version in cmd, here's the trick. Step 5. I created a batch file with
@echo off
:: Switch JDK version
DOSKEY java8=SET PATH=%JAVA_8_HOME%;%PATH%;
DOSKEY java9=SET PATH=%JAVA_9_HOME%;%PATH%
Basically, it disables echo and creates two alias. In batch file any string after :: is the comments. Every time, java8 or java9 is called, it re-exports %PATH% with the new JDK path. Save it as profile.bat. You can name it whatever you want.
Step 6.
Search for regedit (Registry Editor). Click on Edit > New > String Value. Give AutoRun as the Value name and %USERPROFILE%\profile.bat as the Value data. Here, please put your actual path value to the profile.bat we just created. So, whenever a command prompt is opened, it automatically loads profile.bat, which creates those two alias in the script.
Step 7. Close any command prompt you're using or just open a new command prompt. This is because your changes will not affect opened cmd window. Environment changes only happens to new CMD.
Step 8. Verify your results here.
If you're using different Python versions, same trick applies, too. Find my python environment settings here.
I created scripts for every version of Java that I have installed on my PC, and use those whenever I need to change the Java version through the command line. Here is an example of one, should be self-explanatory really:
@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk-10
echo Setting PATH
set PATH=C:\Program Files\Java\jdk-10\bin;%PATH%
echo Display java version
java -version