10

Using OS X Mavericks, and after upgrading my JDK, I can no longer compile with ant.

I've done the usual googling and the vast majority of answers point to JAVA_HOME not being set.

Help me stackoverflow, you're my only hope!

Information which may be useful

$ tail -n1 /etc/profile
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home

$ which javac
/usr/bin/javac

$ ls -lah /usr/bin/javac
lrwxr-xr-x  1 root  wheel    75B  8 Jan 11:23 /usr/bin/javac -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javac

$ ls -lah /System/Library/Frameworks/JavaVM.framework/Versions/
total 80
drwxr-xr-x  13 root  wheel   442B 19 Mar 10:10 .
drwxr-xr-x  12 root  wheel   408B 19 Mar 09:59 ..
lrwxr-xr-x   1 root  wheel    10B  8 Jan 11:23 1.4 -> CurrentJDK
lrwxr-xr-x   1 root  wheel    10B  8 Jan 11:23 1.4.2 -> CurrentJDK
lrwxr-xr-x   1 root  wheel    10B  8 Jan 11:23 1.5 -> CurrentJDK
lrwxr-xr-x   1 root  wheel    10B  8 Jan 11:23 1.5.0 -> CurrentJDK
lrwxr-xr-x   1 root  wheel    10B  8 Jan 11:23 1.6 -> CurrentJDK
lrwxr-xr-x   1 root  wheel    10B  8 Jan 11:23 1.6.0 -> CurrentJDK
lrwxr-xr-x   1 root  wheel    10B 19 Mar 10:10 1.7 -> CurrentJDK
lrwxr-xr-x   1 root  wheel    10B 19 Mar 10:10 1.7.0 -> CurrentJDK
drwxr-xr-x   8 root  wheel   272B  8 Jan 11:23 A
lrwxr-xr-x   1 root  wheel     1B  8 Jan 11:23 Current -> A
lrwxr-xr-x   1 root  wheel    58B 19 Mar 10:10 CurrentJDK -> /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents

$ javac -version -source 1.7 -target 1.7 -fork true
javac 1.7.0_51
javac: invalid source release: 1.7
Usage: javac <options> <source files>
bakoyaro
  • 2,550
  • 3
  • 36
  • 63
davedave
  • 241
  • 1
  • 2
  • 9
  • I have the same version (not the same OS) and it works for me... "Nice" problem you have here. – fge Mar 19 '14 at 00:35

3 Answers3

14

Thanks to @david-w for the effort and helping narrow down the problem.

To solve, I had to

sudo cp $JAVA_HOME/lib/tools.jar /Library/Java/Extensions/

as mentioned here

silly os x

Community
  • 1
  • 1
davedave
  • 241
  • 1
  • 2
  • 9
  • There's no tools.jar file in that path for me. I see stuff like deploy.jar, jce.jar, jconsole.jar, but not tools.jar -- any idea why? – Some Guy Jun 04 '15 at 14:43
2

The solution in the accepted answer didn't work for me; I got the error:

tools.jar: No such file or directory.

However, I found a working solution here. In short, I installed the latest version of the jdk from Oracle website and this solves the problem.

Community
  • 1
  • 1
François Romain
  • 13,617
  • 17
  • 89
  • 123
  • 1
    @KenHampson yes this is another solution to the exact same problem. but this one worked for me and not the accepted one. I don't understand the downvote… – François Romain Aug 09 '14 at 01:56
  • @desgnl: I did not downvote you. I simply added a comment through the review process. The fact that the solution here didn't work for you suggests that it was a similar, but ultimately different issue. – khampson Aug 09 '14 at 02:20
  • @KenHampson The fact that this provides another solution makes it a valid answer. – user207421 Aug 09 '14 at 02:27
  • @EJP: OK, fair enough. – khampson Aug 09 '14 at 02:33
0

I'm on OS X Mavericks too with Java 1.7.0_51 from Oracle. There's no -fork option, but everything else seems to work fine:

HelloWorld.java

public class HelloWorld
{
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Command:

$ javac -version -source 1.7 -target 1.7 -fork true *.java
javac 1.7.0_51
javac: invalid flag: -fork
Usage: javac <options> <source files>
use -help for a list of possible options
$ javac -version -source 1.7 -target 1.7 *.java
javac 1.7.0_51
$   # Everything compiled...

Are you using OpenJDK or the official Oracle release? It may be possible that the OpenJDK doesn't recognize version 1.7 yet. I know that the OpenJDK use to be behind the official Oracle release.

  • What happens if you leave the -source and -target out?
  • What if you have only one or the other? Does it work with 1.6 instead of 1.7?

The error message you get is the same error message I get when I try 1.8. Are you trying to compile from the command line, or from Eclipse, or from Ant?

One more final possibility: The . isn't a period:

$ javac -version -source 1․7 -target 1.7 *.java
javac: invalid source release: 1․7
Usage: javac <options> <source files>
use -help for a list of possible options

That's not a period. It's a small full stop e2:80:a4 and not a full stop/period (2e):

0000000    j   a   v   a   c       -   v   e   r   s   i   o   n       -
           6a  61  76  61  63  20  2d  76  65  72  73  69  6f  6e  20  2d
0000020    s   o   u   r   c   e       1   ․  **  **   7       -   t   a
           73  6f  75  72  63  65  20  31  e2  80  a4  37  20  2d  74  61
0000040    r   g   e   t       1   .   7       *   .   j   a   v   a  \n
           72  67  65  74  20  31  2e  37  20  2a  2e  6a  61  76  61  0a
0000060
David W.
  • 105,218
  • 39
  • 216
  • 337
  • Hi David W (interesting also my name :) To answer your questions, Official Oracle, Using Ant, No targets work. Trying to compile the hello world gives me an insight in to the problem though "$ javac -version *.java \ javac 1.7.0_51 HelloWorld.java:1: cannot access java.lang.Object \ bad class file: /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/rt.jar(java/lang/Object.class) \ class file has wrong version 51.0, should be 49.0:" – davedave Mar 19 '14 at 02:32
  • Solved by "sudo cp $JAVA_HOME/lib/tools.jar /Library/Java/Extensions/" - I'll answer the question and thank you formally once 8hrs has passed – davedave Mar 19 '14 at 03:13