How do I run a particular version of "java"?

Asked by Thomas Leonard on 2012-03-20

I have a program that needs Java 7, so I'd like to run that version of "java". I know I could use update-alternatives, but that affects the user's whole system, which I'd rather not do.

I'm using 0install (from the Ubuntu zeroinstall-injector package). The current Java feed ( says:

  <package-implementation main="/usr/bin/java" package="openjdk-6-jre"/>

but this isn't really right anyway, because "/usr/bin/java" might run a different version. For Java 7, I could use:

  <package-implementation main="/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java" package="openjdk-6-jre"/>

but that hard-codes "amd64" in the path, so it wouldn't work on other architectures. I was hoping for a "/usr/bin/java1.7", or a /usr/lib/jvm/java-7 symlink or similar.

What's the recommended way to run the version of Java from a particular package?

Mailing list post:


Question information

English Edit question
Ubuntu openjdk-7 Edit question
No assignee Edit question
Last query:
Last reply:

can you give the output of:

lsb_release -a; uname -a


No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 11.10
Release: 11.10
Codename: oneiric

Run that lot and you'll have Java from Oracle at 1.7.2

Thanks. But I already know how to reconfigure my users' machines to use Java 7 by default (Java 7 being installed from the Ubuntu package). I also know how to download the upstream tarball and use that.

My question is, how can I write a program that depends on the Ubuntu Java 7 package without reconfiguring the users' machine (since that will affect their other software. which expects to run with Java 6).

And, if there is no easy way to do it now, then how could I extend 0install to make this all work automatically for platform-neutral packages?


it should use the selected java from the dpkg reconfigure command in my script. You can change the java there and the selected one will be used

I don't want to install Java. I want to use the Java 7 that is already installed, without messing up programs using Java 6.

This seems to work:

> JAVA=`dpkg -L openjdk-7-jre-headless|grep '/jre/bin/java$'`
> $JAVA -version
java version "1.7.0_147-icedtea"

But this is harder (and much slower) than it was with Java 6, where I could just do:

> JAVA=/usr/lib/jvm/java-6-openjdk/jre/bin/java
> $JAVA -version
java version "1.6.0_23"

The problem is that the Java 7 path now includes "-amd64", which is architecture-specific. I will use the "dpkg -L" way for now.

Enrico Rosina (e-rosina) said : #7


You simply have to give the path where your version is.

Supposing you are in the folder where you have a jar to run. The command would be

java -jar anyProgram.jar

This command would launch the default java version, the one you see when you type this:
java -version

So now you have to find where is the java version you want to launch, and give the full path of it. For instance:
/usr/lib/jvm/java-6-openjdk/jre/bin/java -jar anyProgram.jar

When you are ready you can also create a link to simplify the command. For instance binding the word "java6" to your java-6-openjdk place.

Was it what you were looking for or is there something that I did not understand in your question???
Thank you to give feedback

Thomas Leonard (talex5) said : #8

Hi Enrico,

I think your idea only works for Java 6, not Java 7, because /usr/lib/jvm/java-7-openjdk/jre/bin/java does not exist.

This is why I think I need to use a launcher script to search for the executable, like this:


If you know a better way (that doesn't involve being root / reconfiguring the user's machine), let me know.


An easier way that does not include querying the dpkg database might be this:

JAVA=$(update-alternatives --list java | grep 'java-7')

Can you help with this problem?

Provide an answer of your own, or ask Thomas Leonard for more information if necessary.

To post a message you must log in.