29

I'm trying to run javac on a Ubuntu terminal. But I get the following:

 $ javac
 The program 'javac' can be found in the following packages:
 * openjdk-6-jdk
 * ecj
 * gcj-4.4-jdk
 * gcj-4.6-jdk
 * gcj-4.5-jdk
 * openjdk-7-jdk
 Try: sudo apt-get install <selected package>

jdk is already installed and running sudo apt-get install openjdk-6-jdk says 0 upgraded, 0 newly installed, 0 to remove and 322 not upgraded.

My jdk is installed in /usr/lib/jvm/java-6-open-jdk; and I'm able to compile and run a java program from eclipse. But I'm having this fore-mentioned problem when using a terminal.

Shuo
  • 4,749
  • 9
  • 45
  • 63
  • But if it's not on PATH I would be expecting something like "command not found". – Shuo Jan 14 '12 at 19:24
  • Ubuntu tries to help you find the command if it isn't currently found. – Dave Newton Jan 14 '12 at 19:27
  • As Dave Newton said, Ubuntu has a database of applications it knows about, you can have a look in the folder /usr/share/command-not-found if interested – Uku Loskit Jan 14 '12 at 19:40
  • Is javac on your path? What output do you get from echo $PATH and echo $JAVA_HOME – Paul Medcraft Jan 14 '12 at 19:09
  • It does not appear on your path directly. In ubuntu there is a two-fold symlink (/usr/bin), first to /etc/alternatives/java, and from there to where it is actually located. if you echo $PATH, you will just get the regular binary directories like /usr/bin etc. – Uku Loskit Jan 14 '12 at 19:12
  • Thanks Uku, that's useful to know. – Paul Medcraft Jan 14 '12 at 19:41

1 Answers1

26

The javac binary (and probably other java binaries) is/are not in your user's $PATH environment variable. There are several ways you can address this:

  1. Add /usr/lib/jvm/java-6-open-jdk/bin to your user's $PATH environment variable. You can do this by adding a line similar to the following in your user's .bash_profile:

    export PATH=${PATH}:/usr/lib/jvm/java-6-open-jdk/bin

    You'll have to restart your terminal session for it to take effect.

  2. Create symbolic links to the java binaries from some directory that's already part of your path (such as /usr/bin)

    sudo ln -s /usr/lib/jvm/java-6-open-jdk/bin/java /usr/bin/
    sudo ln -s /usr/lib/jvm/java-6-open-jdk/bin/javac /usr/bin/

    BTW: There are several other java executables in /usr/lib/jvm/java-6-open-jdk/bin. I've shown the symlink commands for java and javac above. You should run similar command for any other executables you may want to use.

  3. Use the fully qualified path directly on the command line:

    $ /usr/lib/jvm/java-6-open-jdk/bin/javac

Update:

Apparently, there is an elegant, but Ubuntu-specific solution to this problem. When on an Ubuntu system, use update-java-alternatives.

Asaph
  • 159,146
  • 25
  • 197
  • 199
  • Thank you. It fixed my problem. ;P It's weird though that it didn't say "command not found" in the first place. – Shuo Jan 14 '12 at 19:19
  • 2
    Instead of creating the symbolic links by hand, you can just run `update-java-alternatives` to choose the implementation you want (java and javac may not be the only executables you need). – Vincent Zoonekynd Jan 15 '12 at 09:55
  • @Vincent Zoonekynd: Thanks for the tips. I've updated my answer. – Asaph Jan 15 '12 at 15:54
  • Can you futher explain the Ubuntu-specific alternative? I'd love to run it! (Thanks for your help thus far too!) – streetlight Jan 23 '14 at 01:00