How to set default Java version?

  • I followed all the instructions stated at this question, but am encountering some problems with the last part of it.

    I actually have version 6.22 of java and would like to update to version 6.30.

    So after moving the extracted directory java-6-oracle into /usr/lib/jvm I do not know what to do, since the script that is pointed out in the answer above updates from java 5 to java 6.

    For sake of clearness here is output if I do an ls in dir /usr/lib/jvm:

    $ ls -l /usr/lib/jvm
    total 8 
    lrwxrwxrwx  1 root root   14 2011-07-12 15:18 default-java -> java-6-openjdk
    lrwxrwxrwx  1 root root   14 2011-07-12 12:19 java-1.6.0-openjdk -> java-6-openjdk 
    drwxr-xr-x 10 root root 4096 2012-04-12 12:06 java-6.31-oracle 
    drwxr-xr-x  7 root root 4096 2012-02-24 14:43 java-6-openjdk
    

    What should I do now?


    ADDED PART

    Under the suggestion of @fossfreedom I ran the script anyway and actually it updated the java version.

    In fact if I run command java -version output will be the following:

    $ java -version
    java version "1.6.0_31"
    Java(TM) SE Runtime Environment (build 1.6.0_31-b04)
    Java HotSpot(TM) Server VM (build 20.6-b01, mixed mode)
    

    There is still a problem, if Irun javac -version it gives me the old version installed:

    $ javac -version
    javac 1.6.0_22
    

    And if I use the tester at this link it will tell me that version is

    Java Version 1.6.0_22 from Sun Microsystems Inc.
    

    What's going wrong?

    It seems that Java Runtime Environment has updated, but Java Compiler and Java plugin for browser have not.

    How can I update them?


    OTHER ADDED PART

    sudo update-alternatives --config java will return following output

    $ sudo update-alternatives --config java
    There are 2 choices for the alternative java (providing /usr/bin/java).
    
      Selection    Path                                      Priority   Status
    ------------------------------------------------------------
    * 0            /usr/lib/jvm/java-6.31-oracle/bin/java     1062      auto mode
      1            /usr/lib/jvm/java-6-openjdk/jre/bin/java   1061      manual mode
      2            /usr/lib/jvm/java-6.31-oracle/bin/java     1062      manual mode
    
    Press enter to keep the current choice[*], or type selection number:
    

    these makes sense with the fact that JRE is correctly updated to version 6.31, issues are on Java Compiler and Java browser plugin.

    Any ideas?

    @fossfreedom I haven't tried it cause I thought it wasn't a good idea being it explicitely written for uploading from Java 5 to 6, should I anyway?

    hmmm - possibly you may be confusing that the webupd8 script is 0.5b? That is the version of the script - it doesnt refer to the java version. Give it a try. Lets see a screenshot of what the window looks like when you run the script.

    @fossfreedom yeah that totally confused me ;D I ran the script! check the edits in question!thks a lot for helping me..

    how very strange ... never encountered that. Anyway - look at the answer just below - you should be able to `sudo update-alternatives` as per the example.

    @fossfreedom updated again question!

    `javac --version` is for JDK whereas `java --version` is for JRE

    @Matteo - answer updated - you'll need to follow the plugin installation directions as per http://askubuntu.com/questions/67909/how-do-i-install-oracles-jdk-6/89080#89080

    @Ashu - javac --version is JavaCompiler and java --version is for JRE (i think)

  • fossfreedom

    fossfreedom Correct answer

    8 years ago

    Re your first question:

    possibly you may be confusing that the webupd8 script is 0.5b. That is the version of the script - it doesnt refer to the java version.

    Further to the setting of the javac version.

    I suspect you need to explicitly give the path of the javac compiler

    i.e.

    sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/java-6.31-oracle/bin/javac" 1
    

    followed by:

    sudo update-alternatives --config javac
    

    With regards to setting up the java chrome plugin.

    The master question:

    How do I install Oracle JDK 6?

    includes this information - since your folder structure is slightly different your link command should be:

    ln -s /usr/lib/jvm/java-6.31-oracle/jre/lib/i386/libnpjp2.so ~/.mozilla/plugins/
    

    On its own this just changes the compiler - see the answer below for update-java-alternatives which handles switching all of the java-related alternatives, which is 82 on my system.

  • See this; run

    sudo  update-java-alternatives --list
    

    to list off all the Java installations on a machine by name and directory, and then run

    sudo  update-java-alternatives --set [JDK/JRE name e.g. java-8-oracle]
    

    to choose which JRE/JDK to use.

    If you want to use different JDKs/JREs for each Java task, you can run update-alternatives to configure one java executable at a time; you can run

    sudo  update-alternatives --config java[Tab]
    

    to see the Java commands that can be configured (java, javac, javah, javaws, etc). And then

    sudo  update-alternatives --config [javac|java|javadoc|etc.]
    

    will associate that Java task/command to a particular JDK/JRE.

    You may also need to set JAVA_HOME for some applications: from this answer you can use

    export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
    

    for JREs, or

    export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:jre/bin/java::")
    

    for JDKs.

  • I think you should take a look at the update-java-alternatives command from the java-common package and the *.jinfo files used by it. With that you should be able to switch completely between java installations (regardless of JDK, JRE, ...).

    I tried the `update-java-alternatives` and then `java -version` was correct, but the link in `$JAVA_HOME` was still wrong... I changed that one manually, but I wonder whether there is something missing in the `update-java-alternatives` in that regard.

  • On ubuntu/linux you can switch java version using

    update-alternatives --config java
    

    But before, you need install the version.

    You can use this script (./install-java.sh) to install multiple JVMs

    #!/bin/sh
    (
    lookforJdks=$PWD
    echo "Directory: $lookforJdks"
    jdks=`test -e ./javac || find $lookforJdks -type d -iname '*jdk1.*' 2> /dev/null`
    #set -e
    echo 'which jdk do you want to choose? looking for jdks. This might take a while'
    echo "$jdks" | awk '{printf("%5d : %s\n", NR,$0)}'
    read choose
    test -e ./javac || cd `echo "$jdks" | tr '\n' ',' | cut -d',' -f $choose`/bin
    for e in appletviewer extcheck idlj jar jarsigner java javac javadoc javah javap jconsole \
     jdb jhat jinfo jmap jps jrunscript jsadebugd jstack jstat jstatd native2ascii rmic \
     schemagen serialver wsgen wsimport xjc jvisualvm jmc; do sudo update-alternatives \
     --install /usr/bin/$e $e $(readlink -f ./$e) 100; done
    )
    
    echo "RUN update-alternatives --config java"
    

    Put this script in folder where has unpacked the JVM(s), and run:

    /opt/install-java.sh
    

    Next use:

    update-alternatives --config java
    
  • If you are going to manualy set alternatives for javac javaws javadoc or any other java related commands, you might want to make them slaves of the java altenative.

    sudo update-alternatives --install "/usr/bin/java" "java" "/opt/jdk1.8.0_74/bin/java" 0 --slave "/usr/bin/javadoc" "javadoc" "/opt/jdk1.8.0_74/bin/javadoc"
    
    
    update-alternatives: updating alternative /opt/jdk1.8.0_74/bin/java because link group java has changed slave links
    
    
    :/opt/jdk1.8.0_74$ sudo update-alternatives --display java
    

    java - auto mode link best version is /opt/jdk1.8.0_74/bin/java link currently points to /opt/jdk1.8.0_74/bin/java link java is /usr/bin/java slave javadoc is /usr/bin/javadoc /opt/jdk1.8.0_74/bin/java - priority 0 slave javadoc: /opt/jdk1.8.0_74/bin/javadoc

    Change /opt/jdk1.8.0_74/ with the dir where your jdk is installed.

  • Open /etc/environment in any your text editor and add the following line:

    JAVA_HOME="usr/lib/jvm/open-jdk 
    

    - Java path could be diffrent.
    Use source to load the variables, by running this command:

    source /etc/environment
    

    Then check the variable, by running this command:

    echo $JAVA_HOME
    

    With this path set, run this command:

    sudo  update-java-alternatives --list
    

    Choose your default Java Version.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM