4

When I try to compile a project with Maven I get the following error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project webserverlog: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project webserverlog: Compilation failure
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

My JAVA_HOME is pointing to the JDK:

$ echo $JAVA_HOME
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home

$ $JAVA_HOME/bin/javac -version
javac 1.7.0_25
javac: no source files
Usage: javac <options> <source files>

I'm not sure why Maven adds '/jre' to the end of JAVA_HOME, but looking at some other posts this doesn't seem to be the problem:

$ mvn --version
Apache Maven 3.0.4 (r1232337; 2012-01-17 18:44:56+1000)
Maven home: /Users/jerry/dev/springsource/apache-maven-3.0.4
Java version: 1.7.0_25, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.4", arch: "x86_64", family: "mac"

I've searched for a solution to this problem and tried different things for the last couple of days, but can't find anything that works. I've seen some threads with a similar problem, but none of the suggestions worked for me.

Does anyone know how to solve this? Any help greatly appreciated...

Jerry
  • 1,127
  • 2
  • 17
  • 35
  • try to execute this in console: `/usr/libexec/java_home`, what does it return? – Tony Chen Jun 20 '13 at 00:56
  • I get `/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home` – Jerry Jun 20 '13 at 01:33
  • does this ever exist `/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home`? this is different from `/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home`? – Tony Chen Jun 20 '13 at 02:42
  • I have reinstalled OS X and it cleaned out all the previous versions of Java. Now I only have the Oracle 1.7.0_25 version installed. So, when I run `$ /usr/libexec/java_home` I get `/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home`. When I run `$ echo $JAVA_HOME` I get `/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home`. And when I run `$ $JAVA_HOME/bin/javac -version` I get `javac 1.7.0_25 javac: no source files Usage: javac `. Which is now all pointing to the same location. But I still have the same error. – Jerry Jun 20 '13 at 03:08
  • I'm not sure whether this is right, but when I run `$ which javac` I get `/usr/bin/javac`. Where `/usr/bin/javac -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javac` and `/System/Library/Frameworks/JavaVM.framework/Versions/Current -> A`. Therefore, `/usr/bin/javac` points to `/System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/javac`. In any case, Maven should be using JAVA_HOME, right? – Jerry Jun 20 '13 at 03:20
  • It should be. check this out: http://stackoverflow.com/questions/15279586/java-home-in-maven, it seems java home in maven output is right. But checkout my mvn output: `Apache Maven 3.0.3 (r1075438; 2011-03-01 01:31:09+0800) Maven home: /usr/share/maven Java version: 1.6.0_29, vendor: Apple Inc. Java home: /Library/Java/JavaVirtualMachines/1.6.0_29-b11-402.jdk/Contents/Home Default locale: en_US, platform encoding: MacRoman OS name: "mac os x", version: "10.8.3", arch: "x86_64", family: "mac"` which is different from yours. – Tony Chen Jun 20 '13 at 08:24
  • @TonyChen thanks for that link. It makes sense. I've looked at the problem bit more, and I think it relates to the maven compiler plugin. When I specify the source and target versions as 1.7 Maven throws a different error `javac: invalid flag: -s`. If I remove those I get the `Perhaps you are running on a JRE rather than a JDK?` error. BTW, compiling my previous project with Maven worked fine on the 1.6 version, same as yours. I'm just trying to run the Java EE 7 examples from Glassfish 4, so need the JDK 7 to work. I'll see whether it works on a Windows VM. – Jerry Jun 20 '13 at 23:18
  • I finally got this working. Found the solution here http://stackoverflow.com/questions/17099379/builds-failing-after-upgrading-to-java7-missing-tools-jar-and-bad-class-version. Basically copying $JAVA_HOME/lib/tools.jar to /Library/Java/Extensions fixed it. – Jerry Jul 03 '13 at 11:38
  • Good to know that. You can post your answer below and accept that. – Tony Chen Jul 04 '13 at 06:10
  • see http://stackoverflow.com/help/self-answer – willome Jul 08 '13 at 08:14

2 Answers2

5

You should assign the real path to the JDK to the JAVA_HOME variable instead of leaving the CurrentJDK literal. For some reason in Mac OS X the CurrentJDK is not pointing to the actual one regarding Maven. Try something like this in your .profile but replacing you actual jdk folder name:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
Fran Jiménez
  • 176
  • 1
  • 9
  • 3
    You can use `export JAVA_HOME="$( /usr/libexec/java_home )"` to avoid having to update this every time you install a new minor version. – Ian Roberts Nov 26 '13 at 10:53
  • This helped me get around my issue. If other n00bs like me encounter the issue, you need to edit the .profile or .bash_profile file located in your home folder (e.g. `nano ~/.profile`), and add the `export` line in @IanRoberts's comment on its own new line, then press ctrl+x to save and exit the file. – StackExchange What The Heck Mar 19 '14 at 12:05
0

FYI, one cause of "no source files" is lack of disk space wherever tmp points.

Looks like maven and javac store some items in TEMP/TMP/TMDIR and if that director doesn't exist, lacks space, lacks permissions then you can see that problem. at least we did on windows and I suspect it'll happen on better operating systems too.

Peter Kahn
  • 12,364
  • 20
  • 77
  • 135