Diamond shorthand syntax not working javac

Ian2thedv picture Ian2thedv · Mar 20, 2014 · Viewed 7.6k times · Source

When I compile this:

LinkedBlockingDeque<Integer> q = new LinkedBlockingDeque<>();

in Eclipse Java EE Kepler version, everything works fine, but once I try to compile the same program in in the terminal with

javac myProgram.java

in the command line, I receive a "illegal start of type" error, on the <>

I know the Diamond Shorthand came with java 7, so why would the terminal use the javac of java 6 and not 7? And how do I correct this permanently? I'm on Linux, Mint 15.

Running javac -version revealed this

~ $ javac -version
    javac 1.6.0_27

Answer

Aleks G picture Aleks G · Mar 20, 2014

Apparently you have two separate versions of java installed. In Eclipse, you can specify the location of your JDK - which is set to 1.7. However in the terminal, your PATH variable contains (first) the location of JDK 1.6. As it looks like you're running linux/unix, try printing your PATH variable:

$ echo $PATH

You'll see in there path to JDK 1.6; path to JDK 1.7 may also be there but after the JDK 1.6 path. Edit your ~/.profile file and edit the path accordingly - remove JDK 1.6 and add JDK 1.7.

If, on the other hand, path to JDK 1.6 is set on the system level (e.g. in /etc/profile), then the easiest thing to do would be to add this line to your ~/.profile file:

export PATH="/path/to/jdk1.7/bin:$PATH"

After editing this file, log out and log back in.