javac does not work in ubuntu terminal
Asked Answered
W

7

33

When I try to compile a java program with javac, I get an error:

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

When I try java -version, it says,

java version "1.7.0_21"
OpenJDK Runtime Environment (IcedTea 2.3.9) (7u21-2.3.9-1ubuntu1)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)

When I try which java I get /usr/bin/java

How do I compile my java program from commandline?

Wafd answered 28/4, 2013 at 21:7 Comment(3)
Have you tried installing any of those packages?Skylab
Try: sudo apt-get install default-jdkPierpont
What's up with -1 without comment? What is the issue with this question?Septima
S
55

As mentioned by @Blorgbeard you most likely do not have a Java JDK (Java Development Kit) installed. See this web page for more information about the various versions of Java available for Ubuntu.

The following command will install the OpenJDK version of the Java JDK:

apt-get install default-jdk
Septima answered 28/4, 2013 at 21:13 Comment(2)
Perfect, after i ran above command given by @Septima in ubuntu 12.10 for me javac, jps worked fine.Loper
in ubuntu 17.10During
C
13

I have Ubuntu 12.10, with java "1.6.0_27", this command makes javac available:

sudo apt-get install default-jdk

Then compile it like this:

el@apollo:~/retreat3$ javac HelloWorld.java
el@apollo:~/retreat3$ java HelloWorld
Hello, World!
Commissary answered 9/10, 2013 at 9:57 Comment(0)
S
8

In my case, I had the JDK installed, but I'd installed it manually so it wasn't added to the path. Once I added these lines to my .zshrc file (I'm using zsh; if you're using bash, add them to .bash_profile) and relaunched the terminal, it worked:

# Java environment variables
export JAVA_HOME='/opt/jdk/jdk1.8.0_241'  #change accordingly; this needs to match your JDK's location!
export PATH=$PATH:$JAVA_HOME/bin
Southwester answered 22/8, 2017 at 3:22 Comment(0)
C
4

I was having the same problem. I used the following command

sudo apt-get install openjdk-7-jdk

The app compiled after that. Not sure what the difference is to the above solutions. Maybe I wouldn't of had to specify a version.

Coloratura answered 1/2, 2014 at 3:52 Comment(0)
S
2

Actually, java command was working on my ubuntu 16.04 but not javac. At first, I fiddled around with the environment paths and all as i thought i had all java packages installed and the issue was with setting the environment path variable.

Then i tried the command update-alternatives --list java which returned a path but update-alternatives --list javac did not. This explained to me that javac needed to be installed so I ran sudo apt-get install default-jdk

But incase you get a path in running the update-alternatives --list javac then i suggest you should try setting up your $JAVA_PATH or creating symbolic links. Help to that can be found on the page unable to run javac on Ubuntu.

Smear answered 17/9, 2018 at 0:8 Comment(0)
S
0

just copy and paste on your Terminal

sudo apt install openjdk-8-jdk-headless

Seraphic answered 27/7, 2017 at 8:14 Comment(0)
H
0

Had the same problem. Fixed by installing OpenJDK 8 using Yum:

sudo yum install java-1.8.0-openjdk-devel
Headless answered 15/4, 2021 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.