2

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.

Rouven Herzog
  • 110
  • 1
  • 5
  • What happens if you type `which javac`? What does your PATH look like? – Jon Skeet Apr 09 '15 at 09:48
  • 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/Panda3D` – Rouven Herzog Apr 09 '15 at 10:00
  • Download the JDK again. The binary can't be wrong when it says 1.6 – Raffaele Apr 09 '15 at 10:19
  • That's what confuses me the most. But I downloaded the jdk from http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html, 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 Apr 09 '15 at 10:36

5 Answers5

4

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
Zakaria
  • 983
  • 15
  • 24
1

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
Johnny Baloney
  • 3,409
  • 1
  • 33
  • 37
  • That's very handy script, because update-java-alternatives invocation worked only for java and for javac, jar etc it changed to default configuration that was wrong java version. Thank you! – Yuriy Nov 11 '17 at 02:08
0

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

kmandov
  • 3,130
  • 16
  • 15
0

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

Community
  • 1
  • 1
  • Hey Miklos, I set the JAVA_HOME to the 1.8 jdk explicitly already. Therefore echo $JAVA_HOME outputs `/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/`. Still javac -version results in 1.6.0_29. I prepended "/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/" to my $PATH variable as well, still same result. Please see the update for the my path variables. – Rouven Herzog Apr 09 '15 at 10:11
0

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
Shalika
  • 1,457
  • 2
  • 19
  • 38