Serge Ivanoff
Serge Ivanoff

Reputation: 184

Javac version 1.7 not able to build for target 1.7

I am trying to compile Java code, with Sun Java JDK 1.7.0_17, on a Linux Mint system, but I'm getting this problem.

$ javac  -version -target 1.7 
javac 1.7.0_17
javac: invalid target release: 1.7

-target 1.6 doesn't work either. Target 1.5 works, but I get a version problem as such,

$ javac  -version -target 1.5 HelloWorld.java 
javac 1.7.0_17
HelloWorld.java:2: cannot access java.lang.Object
bad class file: /usr/lib/jvm/jdk1.7.0_17/jre/lib/rt.jar(java/lang/Object.class)
class file has wrong version 51.0, should be 49.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
class HelloWorldApp {
^
1 error

Is there somewhere a list of available java targets outside of the sun java directories?

I have no ClassPath or Javahome specified, and setting them doesn't help. jcontrol doesn't help. I have also tried with 1.7.0_15, with similar results.

Upvotes: 3

Views: 9103

Answers (4)

user323094
user323094

Reputation: 3663

Diagnostics:

You can see which java version Maven uses by running "mvn --version"

Solution for Debian:

The mvn script sets the JAVA_HOME env variable internally by looking for javac (which javac). Therefore, if you have multiple java versions installed concurrently, e.g. JDK 6 and JDK 7 and use the Debian Alternatives system to choose between them, even though you changed the alternative for "java" to JDK 7, mvn will still use JDK 6. You have to change the alternative for "javac", too. E.g.:

# update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac

Upvotes: 0

nhoj
nhoj

Reputation: 135

I chased down a similar issue involving Ubuntu, Netbeans and two JDK's: openJDK and oracleJDK. The offending (old and irrelevant) files were eventually located in /usr/java/packages and simply deleted.

I was receiving:

# javac -version hello.java
javac 1.7.0_21
hello.java:3: cannot access java.lang.Object
bad class file: /usr/lib/jvm/java-7-oracle/jre/lib/rt.jar(java/lang/Object.class)
class file has wrong version 51.0, should be 49.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
public class hello {
       ^
1 error

More importantly, I located the offending files with:

# javac -verbose -version hello.java

The -verbose option lists the search path for class files.

Other areas explored during my investigation were:

  • the value of environment variable JAVA_HOME with #echo $JAVA_HOME
  • the value of environment variable PATH with #echo $JAVA_HOME
  • the value of environment variable CLASSPATH with #echo $JAVA_HOME
  • the update-alternatives with #update-alternatives --config javac
  • the update-alternatives with #galternatives
  • the JDK used by Netbeans set in /usr/local/netbeans-7.3/etc/netbeans.conf with the netbeans_jdkhome= switch. Note that this switch overrides update-alternatives

Upvotes: 5

Serge Ivanoff
Serge Ivanoff

Reputation: 184

Update: I reinstalled Mint and the problem went away. Not my preferred option, I should say.

I've yet to compare the differences between the systems.

Can anyone think of how a 1.7 compiler won't build 1.7 code?

Upvotes: 0

CorayThan
CorayThan

Reputation: 17845

I was able to compile a file just fine. I CDed into the directory of the file, then ran

javac -version -target 1.7 App.java 

(App.java is the file name).

I was able to run javac -version and it gave me back:

javac 1.7.0_17

So clearly it knows which version of Java I'm using.

If I run what you did though:

javac -version -target 1.7

I get the exact same message as I do if I run only javac -version:

javac 1.7.0_17

So it seems that running -version and -target 1.7 means that the -target 1.7 gets ignored.

I do have JAVA_HOME specified.

Why do you need to specify -target 1.7 anyway? That should be the default if you're using 1.7.

In your first source code post, did you mean to say "javac HelloWorld.java"? Because that should be the command I would think you would want to run. Not "javac -version -target 1.7".

Upvotes: 0

Related Questions