5

I had successfully installed java on my linux system. When I ran a hello world test sample from desktop it ran successfully, but then after few days when I again tried to run it from terminal using javac command it gave me following response:

The program 'javac' can be found in the following packages:
 * default-jdk
 * ecj
 * gcj-5-jdk
 * openjdk-8-jdk-headless
 * gcj-4.8-jdk
 * gcj-4.9-jdk
 * openjdk-9-jdk-headless
Try: sudo apt install <selected package>

When I test my JAVA_HOME env. variable using echo $JAVA_HOME its showing: /usr/lib/jvm/java-8-openjdk-amd64

its also there with PATH variable. When I ran a which java command its showing: /usr/lib/jvm/java-8-openjdk-amd64/bin/java

and with command whereis java its showing: java: /usr/lib/jvm/java-8-openjdk-amd64/bin/java /usr/share/man/man1/java.1.gz

So, I don't understand the problem, as I, myself applied the env. variables, and its showing everything perfect, then why is not javac command running? can any one look into this problem.

JPG
  • 1,247
  • 5
  • 31
  • 64
  • 2
    Is it possible that you only have the JRE installed (which can run Java software, but cannot compile it), and have not yet installed the JDK (which can compile Java source code into class files)? – Bobulous Aug 19 '17 at 12:42
  • refer this post : https://stackoverflow.com/questions/8864612/unable-to-run-javac-on-ubuntu – Gandhasiri Sandeep Aug 19 '17 at 12:43
  • 1
    What is the output of `which javac`? You're saying that you cannot run `javac` so that would be the first thing to check. Also, how did you install Java? – Erwin Bolwidt Aug 19 '17 at 12:45
  • @ErwinBolwidt output of `which javac` comes out to be nothing! – JPG Aug 19 '17 at 12:48
  • 1
    So you don't have a JDK installed, or it isn't installed correctly. – user207421 Aug 19 '17 at 12:54
  • No, its there. I followed steps from this link https://community.linuxmint.com/tutorial/view/1372 – JPG Aug 19 '17 at 13:16
  • @JSK Well, obviously it (`javac`) is *not* there. Perhaps because the instructions you linked refer to `/opt/java`, but you're referring to `/usr/lib/jvm`. – Andreas Aug 19 '17 at 13:29
  • @Andreas `/usr/lib/jvm/java-8-openjdk-amd64` clearly indicates, its there – JPG Aug 19 '17 at 13:31
  • this is my JAVA_HOME environment variable – JPG Aug 19 '17 at 13:32
  • 1
    @JSK If `/usr/lib/jvm/java-8-openjdk-amd64` is in the `PATH`, like you claim, and it has a `javac`, then `which javac` would show it. Since the `which` command doesn't show it, it appears that `javac` is in fact not in the `/usr/lib/jvm/java-8-openjdk-amd64` folder. – Andreas Aug 19 '17 at 13:34

5 Answers5

9

The core problem is that you do not have the Java Develoment Toolkit (which is different than the Java runtime) installed on your system. This means that utilities used to create Java programs (such as javac, jar, etc) are not on your system.

Thorbjørn Ravn Andersen gave the answer for Debian-family Linux systems, here's the answer for RedHat-family systems

yum install java-devel

or if you are on the latest Fedora

dnf install java-devel
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • I followed this link : https://community.linuxmint.com/tutorial/view/1372 – JPG Aug 19 '17 at 13:17
  • @JSK Glad to hear it. – Edwin Buck Aug 19 '17 at 13:18
  • so what do you say, do I have jdk installed or not – JPG Aug 19 '17 at 13:25
  • @JSK When you ran the command and saw the output you documented, you did not have the JDK installed. If you can run the command `javac -version` and get the version of the `javac` java compiler, you do have the JDK installed. I don't know what your system looks like at the moment; but now you are prepared to test if the JDK is installed yourself. Good luck! – Edwin Buck Aug 19 '17 at 13:29
  • but my JAVA_HOME environment variable clearly indicate, its there : `/usr/lib/jvm/java-8-openjdk-amd64` – JPG Aug 19 '17 at 13:33
  • @JSK Ok, I see the confusion. JAVA_HOME is for your JVM, the `javac` program is not part of your JVM, but is part of your JDK. The JDK is what is used to create Java programs from Java source code. It is not unusual to have the JVM installed without the JDK because more people need to run Java programs than need to write them. You're starting to write Java programs, so you need to install the JDK to get the `javac` command. So having a correct `JAVA_HOME` doesn't mean you have the JDK installed. Running the test above will make sure it is installed. – Edwin Buck Aug 19 '17 at 13:38
  • The situation described in the question is clearly occurring on an Ubuntu system. – Thorbjørn Ravn Andersen Sep 06 '21 at 22:59
  • Thanks, this helped me get up and running on **Fedora 34 ARM64** Parallels Desktop version. I ran `which javac` and it provided me with `/usr/bin/javac`. – The Grindfather May 28 '23 at 20:52
4

Undo your environment settings and run sudo apt install openjdk-8-jdk. javac should now be in your path.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • 1
    My linux install must have been JRE, maven complained, and javac was missing. After this install command, and defining JAVA_HOME in profile it is building! – charo Sep 06 '21 at 20:37
0

Just my 5 cents. On CentOS 7 (assuming you want LTS Java 11):

sudo yum install java-11-openjdk-devel
ivan.ukr
  • 2,853
  • 1
  • 23
  • 41
0

dont worry here is your solution:) first, find where is JVM in your Linux

whereis jvm 

in my case jvm in jvm: /usr/lib/jvm so now go to that directory look at the image and checkout

after coming here

root㉿kali)-[/usr/lib/jvm/java-17-openjdk-amd64/bin]

search ls command look in the image there is not a javac file present

┌──(root㉿kali)-[/usr/lib/jvm/java-17-openjdk-amd64/bin]
└─# apt-get install openjdk-17-jdk

try this if you are using OpenJDK-8 or any different kindly changes the number as your requirement. this command download everything now look what new things come

┌──(root㉿kali)-[/usr/lib/jvm/java-17-openjdk-amd64/bin]
└─# ls                            
jar        javadoc   jdb        jhsdb   jmap      jrunscript  jstatd
jarsigner  javap     jdeprscan  jimage  jmod      jshell      keytool
java       jcmd      jdeps      jinfo   jpackage  jstack      rmiregistry
javac      jconsole  jfr        jlink   jps       jstat       serialver
                                                                           

hurray all stuff is done , learn how to run java in terminal, create hello.java file in desktop, let compile using javac , then run though java ,

┌──(root㉿kali)-[~/Desktop/javac]
└─# javac hello.java
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true


                
                                                                                   
┌──(root㉿kali)-[~/Desktop/javac]
└─# java hello.java 
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
Hello World
nix
  • 1
-1

/usr/lib/jvm/java-8-openjdk-amd64/bin/java

is not an dir, its an excecuteable. try excecute the following to use javac:

/usr/lib/jvm/java-8-openjdk-amd64/bin/javac

or

/usr/lib/jvm/java-8-openjdk-amd64/bin/jstack
Jonas
  • 198
  • 1
  • 14
  • Nobody said it was a directory. `which` reported it as an executable found on the PATH. The words you are looking for are 'executable' and 'execute'. – user207421 Aug 19 '17 at 12:54