2
votes

I'm trying to compile a jar on my mac using 1.8 as a target release, but javac fails with the message "invalid target release".

When I run javac -version it gives me "javac 1.6.0_29".

I have the 1.8 jdk installed under "/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk", but even when I run /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/javac -version it gives me "javac 1.6.0_29".

Sym linking the CurrentJDK folder in "/System/Library/Frameworks/JavaVM.framework/Versions/" to the jdk folder in Library didn't change anything. Neither did sym linking the javac inside /usr/bin to the javac in my jdk folder.

Any ideas how to get javac to use my 1.8 version ?

Thank you very much!

UPDATE

My path variables output the following:

echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/mysql/bin:/opt/X11/bin:/usr/local/git/bin:/Developer/Tools/Panda3D

echo $JAVA_HOME (since I set it already in order to get javac to work) /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/

Preprending /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/ to the $PATH variable didn't change the output for javac -version either.

UPDATE 2

Other question: What does javac do? Is it executing its code directly or depending on other binaries in the system? If so the link for them could be broken. Otherwise I wonder why executing the binary inside my 1.8 jdk folder gives me the version 1.6.

5
What happens if you type which javac? What does your PATH look like?Jon Skeet
which javac points to /usr/bin/javac, echo $PATH results in /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/mysql/bin:/opt/X11/bin:/usr/local/git/bin:/Developer/Tools/Panda3DRouven Herzog
Download the JDK again. The binary can't be wrong when it says 1.6Raffaele
That's what confuses me the most. But I downloaded the jdk from oracle.com/technetwork/java/javase/downloads/…, so it should be fine I assume. I just removed the jdk folder, downloaded the dmg and ran it again. Still, executing the javac from the folder directly with the parameter -version gives me 1.6.0_29..Rouven Herzog

5 Answers

3
votes

The proper Debian/Ubuntu way to configure which javac is pointed to by /usr/bin/javac is to use the update-alternatives command. You can do it interactively, and select from a list of available options:

sudo update-alternatives --config javac

and for java:

sudo update-alternatives --config java
1
votes

In addition to what @Zakaria said, this command will show you all Java packages installed on the system, e.g.:

$ update-java-alternatives -l

java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64
java-1.8.0-openjdk-amd64 1069 /usr/lib/jvm/java-1.8.0-openjdk-amd64

This one an overview of the Java tools and which version will be used, e.g.:

$ update-alternatives --get-selections | grep java
...
java     manual   /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
javac    auto     /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
javadoc  auto     /usr/lib/jvm/java-7-openjdk-amd64/bin/javadoc
...

As shown above here java would run from version 8, and javac from version 7.

If you want to update selections for all of the Java tools in one go try the following replacing the package name with one of your choice:

$ sudo update-java-alternatives -s java-1.8.0-openjdk-amd64

Verify that this has actually worked. If it didn't or partially did you have to resort to a semi-manual way, for example:

$ for i in $(update-alternatives --get-selections | grep java | awk '{system("basename "$3)}'); do sudo update-alternatives --config $i; done
0
votes

where does the /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK link refer to?

Make sure it refers to the proper JDK

/Library/Java/JavaVirtualMachines/jdk{major}.{minor}.{macro[_update]}.jdk

0
votes

Please check that both JAVA_HOME and PATH environment variables point to the 1.8 folder and have no link to the 1.6 java folder, especially the PATH.

Check:

echo %PATH%

Set:

export PATH="java8 folder":$PATH

or check this page: Set environment variables on Mac OS X Lion

0
votes

Do the following steps

  • In the Windows Search bar, type "environment".
  • In your environment variables (account), create a variable called JAVA_HOME. Set it to C:\Program Files\Java\jdk1.8.0_65 (or wherever your JDK is).

  • In your system environment variables, edit PATH. Put this at the beginning of the PATH value: %JAVA_HOME%\bin;

  • Press OK.

  • Exit any command windows you are in.
  • Launch a command window. Type javac -version. you should get this response: javac 1.8.0_65