45

I have an Android Project called Hello on my Ubuntu 10.04 i386 Server (headless). It contains all things an Android project folder should have. I first build the project in bash while in the Project folder using this synax:

./android create project --target 5 --name HelloCompile --path ../../Projects/Hello --activity HelloActivity --package com.code.Hello

then I try to build the .apk with ant like so:

ant debug

I get this error:

BUILD FAILED
/home/myusername/www/sdk/tools/ant/main_rules.xml:384: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "/usr/lib/jvm/java-6-openjdk/jre"

which is very confusing to me because just before I run ant debug I run:

export JAVA_HOME=/usr/lib/jvm/java-6-openjdk

which I know works because printenv in bash shell returns:

JAVA_HOME=/usr/lib/jvm/java-6-openjdk

to compound this, adding this line to my /etc/environment file

export JAVA_HOME=/usr/lib/jvm/java-6-openjdk

does not fix the problem either - I get the same error. Nothing I do changes the fact Ubunut still thinks /usr/lib/jvm/java-6-openjdk/jre is the JAVA_HOME. What is going wrong? I've been at this for too many hours.

lollercoaster
  • 15,969
  • 35
  • 115
  • 173

4 Answers4

79

Did you install the JDK?

When you install Ubuntu only the JRE is installed as part of the default packages. Unfortunately Ubuntu's package management names the directory as if the JRE were installed along with the JDK. The directory is named java-6-openjdk even though the JDK is not be present.

Do the following:

sudo apt-get install openjdk-6-jdk

It will install the JDK in that same directory.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • oh god. I can't believe I missed that - I was sure I installed it...I have no idea how the JDK got there in the first place then....that's really strange. anyway, thanks for getting it right. I'm actually sorry - this was such a silly mistake – lollercoaster Apr 21 '11 at 01:45
  • 6
    It worked out well for me, I didnt have to ask a silly question :) but got the answer –  Feb 28 '13 at 09:37
  • note: the openJDK are not headless. This solution will install a bit chunk of GUI on your server, whether you need it or not. But it is an easy solution :) – foo Sep 06 '13 at 22:48
  • On Debian the issue was resolved for me after installing `default-jdk`. – 0 _ Jul 24 '17 at 20:49
  • 1
    sudo apt-get install openjdk-8-jdk – Badr Bellaj Jul 27 '18 at 11:31
10

--- Updated after noticing a small item in your output ---

You have your JAVA_HOME set to the correct location for a Java Runtime Environment, which unsuprisingly will allow you to run Java programs, but not develop them.

Shorten your JAVA_HOME to /usr/lib/jvm/java-6-openjdk (note the removal of the trailing jre). After that your Ant wrappers / compiler detection code won't get confused, as it will be pointing to the home of your Java Development Environment instead of the embedded, related Java Runtime Environment.

The embedded Java Runtime Environment is provided to make sure you can test against just the core (compiler tools not included) Java offerings.

--- Original post follows ---

Finding the command javac has little to do with JAVA_HOME beyond that javac is typically found in a subdirectory under JAVA_HOME

What you need to do is to modify your PATH environmental variable to include the directory where the Java executables are located. Typically this is done like so

PATH=${PATH}:${JAVA_HOME}/bin
export PATH

but it might be done slightly differently depending on your setup. If you do

ls ${JAVA_HOME}/bin

and you see a javac executable, then the above modification of the path variable will work without any need to change it.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • ok I added those two lines to `/etc/profile`, then logged out of SSH, then logged in and tried to run `ant debug` again. exact same error. – lollercoaster Apr 20 '11 at 21:20
  • 1
    @lollercoaster I'd put money that something like this is the problem. If you do `echo $path` what comes out? Also, what happens for `which javac`? – corsiKa Apr 20 '11 at 21:24
  • Ok, I notices a slight error, you're pointing JAVA_HOME to the JDK's embedded runtime environment, instead of pointing it to the development environment. The embedded runtime environment lacks compilation tools; because, well it's to test that you can run (not to compile code). – Edwin Buck Apr 20 '11 at 21:35
  • type 'locate javac' find the path to the compiler bin directory and add to your $PATH variable. If you get no result you've only installed the JRE and not the JDK. – nsfyn55 Apr 20 '11 at 23:10
  • 1
    `username@myserver:~$ locate javac /etc/alternatives/javac /etc/alternatives/javac.1.gz /usr/bin/javac /usr/lib/jvm/java-1.5.0-sun-1.5.0.22/bin/javac /usr/lib/jvm/java-1.5.0-sun-1.5.0.22/man/ja/man1/javac.1.gz /usr/lib/jvm/java-1.5.0-sun-1.5.0.22/man/man1/javac.1.gz /usr/share/man/man1/javac.1.gz /usr/share/vim/vim72/compiler/javac.vim /usr/share/vim/vim72/syntax/javacc.vim /var/lib/dpkg/alternatives/javac` – lollercoaster Apr 21 '11 at 01:43
  • `which javac` returns `/usr/bin/javac` – lollercoaster Apr 21 '11 at 01:46
  • In ubuntu, odds are +95% that /usr/bin/javac is a soft link to /etc/alternatives/javac which is a soft link to /usr/java//bin/javac. Such a pattern of symlinks is indicative of using the alternatives system. – Edwin Buck Apr 21 '11 at 15:32
  • All of this is irrelevant. The problem is that it's not finding a class com.sun.tools.javac.Main, which is in the JDK version of tools.jar. That's not the same as the javac binary. – cbare Oct 25 '11 at 16:33
2

As Edwin Buck stated, check your $PATH for softlinks to /etc/alternatives/java in the /usr/bin/ directory. They are being read before your appended JAVA_HOME variable.

That was my problem:

ls -al /usr/bin/j* 
lrwxrwxrwx 1 root root    22 2012-05-07 13:26 /usr/bin/java -> /etc/alternatives/java
lrwxrwxrwx 1 root root    25 2011-05-12 19:45 /usr/bin/java_vm -> /etc/alternatives/java_vm
lrwxrwxrwx 1 root root    24 2011-05-01 05:22 /usr/bin/javaws -> /etc/alternatives/javaws
lrwxrwxrwx 1 root root    26 2011-05-12 19:45 /usr/bin/jcontrol -> /etc/alternatives/jcontrol
lrwxrwxrwx 1 root root    23 2011-04-26 02:24 /usr/bin/jexec -> /etc/alternatives/jexec
Perception
  • 79,279
  • 19
  • 185
  • 195
garec
  • 487
  • 5
  • 7
1

Changing JAVA_HOME and PATH are insufficient.

After installing the Java JDK version that you want (Java DEVELOPMENT Kit, not just Java Runtime Environment JRE), change your preferred version with sudo update-alternatives --config java. If you're on Ubuntu, you probably have 1.6 and 1.7 installed, and 1.8 is available in the PPAs (though I can't find a PPA of 1.8 that's not old).