32

I am trying to follow a tutorial about how to use ant to build and run your application. I've followed all the steps and have created the build file, but when I try to run ant it gives me this error.

BUILD FAILED /home/bilal/tmp/ant/build.xml:19: 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"

Any ideas how to resolve this issue ?

comatose
  • 1,952
  • 7
  • 26
  • 42

16 Answers16

37

Make JAVA_HOME variable point to a jdk installation, not jre.

You are referencing the runtime environment, not the development kit - it can't find the compiler because its not there.

From the line you posted, which states you have open-jdk you can just remove the jre at end:

export JAVA_HOME='/usr/lib/jvm/java-6-openjdk/'
Alastair
  • 6,837
  • 4
  • 35
  • 29
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • and how do I find where is the jdk installation ? – comatose Jun 28 '12 at 13:38
  • :) well it was pre-installed in my mac, is there a command like which java or something like that , which could tell me where is it installed ? – comatose Jun 28 '12 at 13:40
  • 5
    it already tells you in the error message, the current path is "/usr/lib/jvm/java-6-openjdk/jre". just remove the "jre" at the end. – kritzikratzi Jun 28 '12 at 13:40
  • 20
    after I do this export JAVA_HOME=/usr/lib/jvm/java-6-openjdk/ it still throws the same error, stating that it is currently set to "/usr/lib/jvm/java-6-openjdk/jre" – comatose Jun 28 '12 at 13:45
  • @comatose check your build file, is that setting java_home ? I know nothing about macs (except they are expensive) is that how you permanently set a system wide environment variable ? Nice username btw – NimChimpsky Jun 28 '12 at 13:46
  • yes, I think so because when I set the path to export JAVA_HOME=/usr/lib/jvm/java-6-openjdk/bin/ then it gives this error "Error: JAVA_HOME is not defined correctly. We cannot execute /usr/lib/jvm/java-6-openjdk/bin/java" – comatose Jun 28 '12 at 13:50
  • echo $JAVA_HOME will tell you what it is set at, check your spellings, and I guess check that the bin directory does contain the javac program – NimChimpsky Jun 28 '12 at 13:52
  • its /usr/lib/jvm/java-6-openjdk/bin/, since I changed this, changing it back to jre gives /usr/lib/jvm/java-6-openjdk/jre – comatose Jun 28 '12 at 13:54
  • it should be this /usr/lib/jvm/java-6-openjdk/ – NimChimpsky Jun 28 '12 at 13:55
  • changing it to /usr/lib/jvm/java-6-openjdk gives /usr/lib/jvm/java-6-openjdk when I echo $JAVA_HOME, but still when I run ant it says JAVA_HOME is curerntly set to /usr/lib/jvm/java-6-openjdk/jre – comatose Jun 28 '12 at 13:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13167/discussion-between-nimchimpsky-and-comatose) – NimChimpsky Jun 28 '12 at 13:57
  • 16
    This was marked as solution, but it doesn't provide a solution. – Vincent Cantin Jun 26 '13 at 12:08
  • 10
    This answer doens't give the solution, why is it accepted and upvoted? – quinestor Jul 10 '13 at 13:10
  • 2
    @NimChimpsky I just solved the issue in Ubuntu12.04 based on ingyhere's answer. Changin JAVA_HOME doesn't solve the issue because ant will not use that value and alawys show that it points to jre subfolder – quinestor Jul 10 '13 at 13:16
  • 1
    Miracle! After 10hrs, this worked for me: sudo apt-get remove openjdk-6-* – Kasra Apr 26 '14 at 23:57
  • Check your folder name in '/usr/lib/jvm'. e.g., Use it >export JAVA_HOME='/usr/lib/jvm/java-7-openjdk-amd64/' – KyungHoon Kim Sep 05 '14 at 16:09
  • This accepted answer doesn't provide a solution. @KyungHoonKim It doesn't solve the issue as well. – Omar Shabab Apr 18 '21 at 14:47
25

You installed java...

apt-get install default-jre

But not the JDK...

apt-get install default-jdk
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
user2708687
  • 259
  • 3
  • 2
  • 1
    @ingyhere Installing `default-jdk` as suggested here fixed the problem for me. – Mark Stosberg Jan 07 '15 at 15:39
  • @Mark Stosberg First, I deleted my earlier comment because it was meant to follow up comments to another answer. For posterity, also, the context is not clear on this page. So, the idea is that Ant should run within a JRE, irrespectively. To compile an app, though, a JDK is required. The Ant Javac task should (or at least used to) default to finding the Java compiler (javac) by sniffing internal properties. So, if the Javac "executable" attribute isn't specified explicitly it could find the wrong thing. Having a JDK installed is prerequisite, and yes, of course, not having it is a problem. – ingyhere Jan 07 '15 at 22:29
13

This is by design. You cannot use ant's java.home (which is a java.lang.System property) interchangeably with how JAVA_HOME is set in the OS environment. You are probably trying to assert the location of the Java compiler with a fundamentally different value from a different property layer -- i.e. java.home (from Ant's Java internals) points to the Java Runtime Environment at <any_installed_java_pointed_to_by_ant>/jre while JDK_HOME (from the OS environment) is usually set to <DOWNLOADED_AND_INSTALLED_JAVA_DEVELOPMENT_KIT>.

See my question and answer here for more details: Where does Ant set its 'java.home' (and is it wrong) and is it supposed to append '/jre'?

The solution is to access the system environment property within Ant by using ${env.JAVA_HOME}. Specify which java to use explicitly in the Javac Task by setting the executable property to the javac path and the fork property to yes (see Ant's Javac Task Documentation). That way, it doesn't matter what Java environment Ant is running inside, the compiler is always clearly specified!

Community
  • 1
  • 1
ingyhere
  • 11,818
  • 3
  • 38
  • 52
  • 3
    I wouldn't say this is the solution. It explains what's going on, not how to fix it. I'm having the same issue ant it's Ant, not I, that seems to be using java.home and JAVA_HOME interchangeably. My JAVA_HOME is correct, yet Ant seems to be appending /jre and then complaining about it! – Dave Rager Apr 15 '14 at 20:38
  • @Dave Rager: Ant is functioning *as designed*. – ingyhere Apr 16 '14 at 01:07
  • 2
    I'm not using java.home Ant is. And it's telling me my JAVA_HOME is incorrect and giving me its java.home as the value for it. That seems pretty broken to me – Dave Rager Apr 16 '14 at 16:31
  • http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html -- "Installation directory for Java Runtime Environment (JRE)." Note that it does not say Java Development Kit. It's detected by the system based on the VM where the application is executing. – ingyhere Oct 30 '14 at 18:35
5

I know this question is old but the accepted answer does not work anymore and since this is the fist link on google search i'll tell how i solved this problem.

for eclipse using ubuntu:

go to Window->Preferences->Ant->Runtime->Select Ant_Home_Entries and click on add external jars then find in file explorer where your jdk is (default is in /usr/lib/jvm/) and in the lib folder of your jdk you will find the tool.jar. select this one and click apply.

try to build your project and things should work!

note: i hadn't used ant for a long time but needed it for ycsb couchbase workload generator (http://www.couchbase.com/wiki/display/couchbase/Load+Generator+Setup) if anyone is/was stuck on this.

dtrihinas
  • 446
  • 1
  • 4
  • 11
3

It looks like you are currently pointing JAVA_HOME to /usr/lib/jvm/java-6-openjdk/jre which appears to be a JRE not a JDK. Try setting JAVA_HOME to /usr/lib/jvm/java-6-openjdk.

The JRE does not contain the Java compiler, only the JDK (Java Developer Kit) contains it.

Michael
  • 2,460
  • 3
  • 27
  • 47
2

I am using Windows 7 and have struggled with the same issue. I fixed it by changing my environment variables.

To change your environment variables click here

I added ";%JAVA_HOME%/bin" to the end of paths variable and added a new "JAVA_HOME" variable and set its value to the location of my JDK "C:\Program Files\Java\jdk1.8.0_11". After that I restarted my Node.js command prompt and it worked.

Please note you JDK directory may be different then mine. Also depending on your setup, you may need to restart you computer after setting the environment variables.

1

The JAVA_HOME you have above only points to the JRE, which does not contain the compiler. Set your JAVA_HOME to

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

...and try again.

Jaco Van Niekerk
  • 4,180
  • 2
  • 21
  • 48
1

I had a similar problem and it turned out the issue was having both versions 6 & 7 of OpenJDK. The answer comes from r-senior on ubuntu forums (http://ubuntuforums.org/showthread.php?t=1977619) --- just uninstall version 6:

sudo apt-get remove openjdk-6-*

make sure that JAVA_HOME and CLASSPATH aren't set to anything since that isn't actually the problem.

1

for centos yum -y install java-1.7.0-openjdk-devel.x86_64

and update JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk.x86-64

uday
  • 11
  • 1
1

I just copied tools.jar file from JDK\lib folder to JRE\lib folder. Since then it worked like a champ.

  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). - [From Review](/review/low-quality-posts/10687279) – mjp66 Dec 26 '15 at 08:31
  • @mjp66, did you try. It didn't provide to the answer the question? How come? We don't need clarification from not only an author anyone who have been facing issue can clarify I believe. And what is the sufficient reputation??? – Shiva Krishna Chippa Dec 27 '15 at 06:11
  • This is the best answer! It worked and did not require commands that I had no idea where I'm supposed to write – Maude Jul 28 '17 at 22:52
0

Experienced this issue when trying to run the android emulator with Meteor 1.0 on elementary OS Luna (based on Ubuntu 12.04 LTS sources).

openjdk-6-jdk was installed, as well as the jre. In the end, not expecting any success, I tried:

sudo apt-get remove openjdk-6-*

this resulted in fully expected errors, so I followed up with

sudo apt-get install openjdk-6-jdk

and things worked. Go figure.

keito
  • 21
  • 3
0

On Ubuntu 14.04, I found two parts to solving the problem:

  1. Remove /jre from the environment variable. For me: export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
  2. Install the JDK as well as the JRE: sudo apt-get install default-jdk
Mark Stosberg
  • 12,961
  • 6
  • 44
  • 49
0

Once you update the JAVA_HOME path as stated in the answer, you should do this:

$source ~/.bashrc

This will refresh bashrc show the updated path in $JAVA_HOME when you check again.

user3270763
  • 125
  • 1
  • 3
  • 13
0

Execute:

$ export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-3.b16.el6_9.x86_64

and set operating system environment:

vi /etc/environment

Then follow these steps:

  1. Press i
  2. Paste

    JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-3.b16.el6_9.x86_64
    
  3. Press esc

  4. Press :wq
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
0

I met this issue in rhel, my "JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk"(which is a symbolic link), and ant complains.

MY solution for this is to use the real jdk path in JAVA_HOME, like:

JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64

It works for me.

h.chen
  • 1
  • 2
0

Under Jenkins it complains like :

/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/ doesn’t look like a JDK directory

Reason : Unable to found any Dev Kit for JDK.

Solution:

Please make sure to install openjdk-devel package as well along with your JDK-1.8* version and reexport with : # source ~/.bash_profile

Sid_R
  • 11
  • 1