Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to run javac on Ubuntu [closed]

Tags:

java

javac

ubuntu

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.

like image 535
Shuo Avatar asked Jan 14 '12 19:01

Shuo


People also ask

Why javac is not working in Ubuntu?

For 'javac' you neeed the JDK (Java Development Kit). Ubuntu doesn't come with JDK pre-installed, only the JRE. So, you have the 'java' command, but not the 'javac' command. Just install one of the packages recommended below and javac will work again.

Why my javac is not working?

javac is not recognized is an error occurs while we compile the Java application. It is because the JVM is unable to find the javac.exe file. The javac.exe file is located in the bin folder of the JDK. The reason behind to occur the error is that the PATH is not added to the System's environment variable.

Where is javac installed Ubuntu?

Oracle Java is located at /usr/lib/jvm/java-11-oracle/jre/bin/java .


1 Answers

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.

like image 184
Asaph Avatar answered Sep 22 '22 22:09

Asaph