49

Ubuntu seems to favour OpenJDK/JRE very much over Sun JDK/JRE. Even after I installed Sun JRE, JDK and plugin and spent some time plucking out OpenJDK-related packages, apt-get has installed them back with some packages as a dependency. Can this behaviour be corrected in favour of Sun Java packages? I'd like to have one and only Java stack installed (yes, it's a bit of OCD, but I like to have my systems clean) and want it to be Sun Java.

Update: as Marcos Roriz notes, the problem seems to be in default-jre (on which Java-dependent packages use to depend) pointing to OpenJDK, so the question seems to go about how to hack default-jre/default-jdk to point to Sun Java.

Luis Alvarado
  • 211,503
Ivan
  • 57,065

10 Answers10

35

Installing Sun Java on Older Versions of Ubuntu (9.10 and below)

I had the same problem once about three years ago with Ubuntu 8.04 I think. There was a weird problem when using openjre and the problem was solved by using the sun jre version. Here are the steps in the terminal which apply to versions below 10.04, since starting from 10.04 sun java has been dropped and replaced by openjre:

sudo apt-get remove openjdk-6-jre default-jre default-jre-headless

sudo apt-get install sun-java6-jre sun-java6-bin sun-java6-plugin sun-java6-fonts

(Everything Sun java related. Just to make sure)

After that, if you removed openjdk then you would only have sun java. If you did not remove it then you have both of them. To change from one to another do this:

sudo update-java-alternatives -l - This will show you the list of java engines you can use.

The name of the java alternative is the one in the left so for example openjdk would appear like this:

java-6-openjdk 1061 /usr/lib/jvm/java-6-openjdk

And you would set the system to openjdk like this:

sudo update-java-alternatives -s java-6-openjdk

If you would want to use Sun then you would do this (After installation of the sun packages):

sudo update-java-alternatives -s java-6-sun

If you want to use the java 7 version, simply change the number 6 mentioned above for 7. For example if I wanted to install the openjre version 7 I would:

sudo apt-get install openjdk-7-jre default-jre default-jre-headless

Installing Sun Java on Newer versions of Ubuntu (10.04 an above)

Open the terminal and type the following:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update   
sudo apt-get upgrade   
sudo apt-get install oracle-java7-installer

That should install the Sun Java version on your system. To change to it simply do the following in case you have other java alternatives:

sudo update-java-alternatives -s java-7-oracle  

If you want the installation to be automatic type the following:

sudo echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true | sudo /usr/bin/debconf-set-selections

after that simply do the following to set the default environment variables:

sudo apt-get install oracle-java7-set-default

Note that the PPA includes versions 6, 7 and 8. So you can install any of them by simply changing the number in the installation line, for example:

sudo apt-get install oracle-java8-installer

Would install Java 8. After installing the Java version you wish to use and setting the version to the default with the update-java-alternatives command, you can check which version you are using by typing the following:

java -version

It will show you the default version you are using at that moment.

I would like to also mention that OpenJRE/JDK has come a long way from 2 or 3 years ago when it had many issues that were solved by changing to Sun Java.

Luis Alvarado
  • 211,503
11

I think the problem is that some Java packages rely on default-jre which points to openjdk-jre. I don't think there is a easy way to permanetely uninstall openjdk. The thing is that you probably will bump on openjdk again when you install a java package/program.

But if you're willing to have both, is it easy to set the java runtime programs (java, javah, javap, and so on) to sun implementation. Do it:

sudo apt-get install sun-java6-jre sun-java6-plugin sun-java6-fonts

or if you are a java dev (install the sdk and srcs):

sudo apt-get install sun-java6-jdk sun-java6-jre sun-java6-source

And set it as the default programs with:

sudo update-java-alternatives -s java-6-sun
  • 1
    Yes, I believe this is about default-jre. No, I really don't want to have both Sun and Open JDKs - this is what's the question about - to have one and only and let it be Sun JDK. Is there a way to hack default-jre to point to Sun JRE? – Ivan Jan 12 '11 at 00:05
  • Yep, I'll try to rebuild the default-jre package to point to sun-java this evening. Right now I'm @ work. – Marcos Roriz Junior Jan 12 '11 at 11:46
6

Delete openjdk and icedtea:

apt-get purge openjdk-\* icedtea-\* icedtea6-\*

add the following to /etc/apt/sources.list

deb http://ppa.launchpad.net/webupd8team/java/ubuntu YOUR_UBUNTU_VERSION_HERE main

install Sun/Oracle JRE or JDK:

apt-get install oracle-jdk7-installer
apt-get install oracle-java7-installer 
  • 2
    And after this you are left with no Java at all... The question actually refers to who to replace one with the other, not how to remove OpenJDK from your system. Add a bit more information to your answer, the question cannot be answered only by following these steps. – Bruno Pereira Apr 18 '12 at 15:25
6

The .jdk1.6.0_23.jinfo file should be a list of the supplied programs and their paths. There is a descriptive header followed by a blank line. Then a list of the programs in a three field format: entry type (jre/jkd/plugin); program name; and path. You should have some jinfo files you can use to see what the contents should look like. This is likely used by the alternatives mechanism to determine which links to create. The head of my Sun package is.

name=java-6-sun-1.6.0.22
alias=java-6-sun
priority=63
section=non-free

jre ControlPanel /usr/lib/jvm/java-6-sun/jre/bin/ControlPanel
jre java /usr/lib/jvm/java-6-sun/jre/bin/java
jre java_vm /usr/lib/jvm/java-6-sun/jre/bin/java_vm

If you want to override the default Java commands without using the alternatives mechanism put your Java path ahead of /usr/bin in your path.

When doing a manual install, I install Java in /opt. for your install I would have installed in /opt/jdk1.6.0_23. Then I put the path $JAVA_HOME/bin at the start of my PATH variable.

EDIT: This is the whole Sun file. Note .java-6-sun.jinfo is a symbolic link to .java-6-sun-1.6.0.22.jinfo.

name=java-6-sun-1.6.0.22
alias=java-6-sun
priority=63
section=non-free

jre ControlPanel /usr/lib/jvm/java-6-sun/jre/bin/ControlPanel
jre java /usr/lib/jvm/java-6-sun/jre/bin/java
jre java_vm /usr/lib/jvm/java-6-sun/jre/bin/java_vm
jre javaws /usr/lib/jvm/java-6-sun/jre/bin/javaws
jre jcontrol /usr/lib/jvm/java-6-sun/jre/bin/jcontrol
jre keytool /usr/lib/jvm/java-6-sun/jre/bin/keytool
jre pack200 /usr/lib/jvm/java-6-sun/jre/bin/pack200
jre policytool /usr/lib/jvm/java-6-sun/jre/bin/policytool
jre rmid /usr/lib/jvm/java-6-sun/jre/bin/rmid
jre rmiregistry /usr/lib/jvm/java-6-sun/jre/bin/rmiregistry
jre unpack200 /usr/lib/jvm/java-6-sun/jre/bin/unpack200
jre orbd /usr/lib/jvm/java-6-sun/jre/bin/orbd
jre servertool /usr/lib/jvm/java-6-sun/jre/bin/servertool
jre tnameserv /usr/lib/jvm/java-6-sun/jre/bin/tnameserv
jre jexec /usr/lib/jvm/java-6-sun/jre/lib/jexec
jdk HtmlConverter /usr/lib/jvm/java-6-sun/bin/HtmlConverter
jdk appletviewer /usr/lib/jvm/java-6-sun/bin/appletviewer
jdk apt /usr/lib/jvm/java-6-sun/bin/apt
jdk extcheck /usr/lib/jvm/java-6-sun/bin/extcheck
jdk idlj /usr/lib/jvm/java-6-sun/bin/idlj
jdk jar /usr/lib/jvm/java-6-sun/bin/jar
jdk jarsigner /usr/lib/jvm/java-6-sun/bin/jarsigner
jdk javac /usr/lib/jvm/java-6-sun/bin/javac
jdk javadoc /usr/lib/jvm/java-6-sun/bin/javadoc
jdk javah /usr/lib/jvm/java-6-sun/bin/javah
jdk javap /usr/lib/jvm/java-6-sun/bin/javap
jdk jconsole /usr/lib/jvm/java-6-sun/bin/jconsole
jdk jdb /usr/lib/jvm/java-6-sun/bin/jdb
jdk jhat /usr/lib/jvm/java-6-sun/bin/jhat
jdk jinfo /usr/lib/jvm/java-6-sun/bin/jinfo
jdk jmap /usr/lib/jvm/java-6-sun/bin/jmap
jdk jps /usr/lib/jvm/java-6-sun/bin/jps
jdk jrunscript /usr/lib/jvm/java-6-sun/bin/jrunscript
jdk jsadebugd /usr/lib/jvm/java-6-sun/bin/jsadebugd
jdk jstack /usr/lib/jvm/java-6-sun/bin/jstack
jdk jstat /usr/lib/jvm/java-6-sun/bin/jstat
jdk jstatd /usr/lib/jvm/java-6-sun/bin/jstatd
jdk native2ascii /usr/lib/jvm/java-6-sun/bin/native2ascii
jdk rmic /usr/lib/jvm/java-6-sun/bin/rmic
jdk schemagen /usr/lib/jvm/java-6-sun/bin/schemagen
jdk serialver /usr/lib/jvm/java-6-sun/bin/serialver
jdk wsgen /usr/lib/jvm/java-6-sun/bin/wsgen
jdk wsimport /usr/lib/jvm/java-6-sun/bin/wsimport
jdk xjc /usr/lib/jvm/java-6-sun/bin/xjc
plugin xulrunner-1.9-javaplugin.so /usr/lib/jvm/java-6-sun/jre/lib/amd64/libnpjp2.so
plugin mozilla-javaplugin.so /usr/lib/jvm/java-6-sun/jre/lib/amd64/libnpjp2.so
BillThor
  • 4,698
  • I don't have any java installed so I don't have anything related to it, nothing installed, no .jinfo files etc. I created the usr/lib/jvm folder myself because I read that that's where it's installed by default, and I'm going to keep it that way. That's why I would like to see the full content of the .jinfo file for the Sun's JDK. – jurchiks Feb 13 '11 at 11:18
  • I've added the full sun .jinfo file. – BillThor Feb 13 '11 at 15:58
4

I added this line to the /etc/environment file: JAVA_HOME="/usr/lib/jvm/jdk1.6.0_23" and added JAVA_HOME/bin to the PATH variable, BUT the OS still doesn't recognise the command java, says it's not installed and offers me gcj and openjdk.

Did you check it?

echo $JAVA_HOME 
echo $PATH

If you don't need it: Deinstall openjdk and gcj in synaptic or with apt-get.

apt-get remove gcj 
apt-get remove openjdk

I would ban java from alternatives then, and do mostly as BillThor suggested, why I gave him an upvote.

which javac
which java

should point to where you installed java, and where it is found in the PATH.

user unknown
  • 6,507
  • I don't have gcj or openjdk installed... Just tried the which commands and it prints nothing. Found out that I forgot the $ symbol before the JAVA_HOME variable in PATH, I'll restart now and see how it goes. – jurchiks Feb 13 '11 at 11:19
  • Restarted the PC, but the which command still shows nothing. Both JAVA_HOME and PATH variables are correct:
    echo $JAVA_HOME
    /usr/lib/jvm/jdk1.6.0_23
    echo $PATH
    $JAVA_HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
    Where could be the problem? Edit: GOD DAMN IT, why are there no line breaks in comments??
    – jurchiks Feb 13 '11 at 11:53
  • a) restarting the PC shouldn't be required. Reopen the shell should suffice, or mostly relogin as user. b) Look inside the help for formatting in comments. I see echo $PATH $JAVA_HOME/bin:/usr/local/sbin... but in the result $JAVA_HOME must be evaluated - there is something wrong, or your reporting is mistakable. c) Linebreaks: They aren't there, so that we constantly hit ENTER too early, and then the clock ticks, because editing is just for few minutes allowed - I constantly step into that trap. I hate it! :) – user unknown Feb 13 '11 at 12:38
  • Well, the JAVA_HOME variable is in the same file (/etc/environment), just in the first line. Maybe it should be somewhere else? It recognises it when I type echo $JAVA_HOME though... – jurchiks Feb 13 '11 at 14:16
  • My question was: Does echo $PATH print $JAVA_HOME... or does it print the value for JAVA_HOME, i.e.: /usr/lib/jvm/jdk1.6.0_23 ? – user unknown Feb 14 '11 at 00:37
  • No it doesn't, you already saw it. But where's the problem? – jurchiks Feb 14 '11 at 10:29
  • After configuring the .jinfo file the update-java-alternatives -l now displays the path correctly, but when trying to use update-java-alternatives -s jdk1.6.0_23 it gives me a long list of update-alternatives: error: no alternatives for %insert name here%. And the command java still says the same: "The program 'java' can be found in the following packages..." – jurchiks Feb 14 '11 at 10:41
  • a) To your first answer: An either-or-question shouldn't be answered with yes or no. I still don't know what your answer is. b) The update-alternatives mechanism is useful, when you have more than one version in parallel, and need switching. I thought you deinstalled gcj and openjdk, and just have one sun-java now. – user unknown Feb 14 '11 at 13:37
  • a) I don't understand what you do not understand. I already wrote that echo $PATH prints out $JAVA_HOME, not /usr/lib/jvm/jdk1.6.0_23, i.e. it doesn't resolve the path. b) I never had gcj/openjdk, this is an almost clean install from a CD (not DVD) with only few programs and updates installed. – jurchiks Feb 14 '11 at 14:26
  • But I asked 'does it print a or does it print b?' and you answered: 'No, it doesn't'. Okay - that's clear now. 2) You never had gcj? So 'offers me' meant 'offers me to install ...', not 'offers me to execute'? OK. Cleared that too. 3) $JAVA_HOME with $ in the PATH is very suspicious. But I'm not sure who reads /etc/environment and how, so, please remove the JAVA_HOME definition, and substitute it with the /usr/lib/jvm/jdk1.6.0_23/bin: in the PATH.
  • – user unknown Feb 15 '11 at 00:22
  • Ok, did it and of course it works that way, but afaik it should have worked the other way too. – jurchiks Feb 15 '11 at 11:14
  • Yes, it should, but without deeper knowledge of the dependencies between conf/.rc-files in etc it will take too long to fiddle it out here, via ask-ubuntu. – user unknown Feb 16 '11 at 07:02