Diamond shorthand syntax not working javac - java

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

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.

You haven't added the Java 7 bin directory to your PATH environment variable. Or maybe Java 6 is also in there and is before the Java 7 path.

In eclipse.. Go to project properties -> Java Compiler and select the required java compiler version.

Related

JDK 11 as JAVA_HOME?

Can the JAVA_HOME environment variable refer to JDK 11 (e.g. jdk-11.0.8) while running Codename One applications under Eclipse 2020-06?
Or does it need to point to JDK 8 (e.g. jdk-1.8.0_241)?
I am having the following error when I try to run Hello World app under Eclipse 2020-06 when JDK 11 is set to JAVA_HOME:
BUILD FAILED
...[shortened for brevity]...\HiWorldPrj\build.xml:106: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\Program Files\Java\jre1.8.0_241"
I am positive JAVA_HOME points to JDK 11 (because I can echo it from command line).
But I think I got this error because tool.jar is removed in JDK versions 9 and up and com.sun.tools.javac.Main used to be under tools.jar?
Yes you can use JDK 11 but your problem seems that the JAVA_HOME is set to the JRE and not the JDK.
Check this answer. The JRE alone cannot compile Java code.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\Program Files\Java\jre1.8.0_241"
Find where your JDK 11 directory is and set the JAVA_HOME environment variable to that path.
However I recommend that you set the Java in the Eclipse configuration tab. You'll find that at "Windows menu -> Java -> Installed JREs".
Update 1
It seems that Codename One uses JAVA_HOME, it is set up in the build.xml. So forget what I said about the Eclipse variables for now.
<property name="javaHome" value="${env.JAVA_HOME}"/>
Make sure your environment variable JAVA_HOME is set to the JDK 11. You can see dos on how to set that here.
Update 2
Also, you'll need to update every source="1.5" and target="1.5" in the build.xml file to either 1.8 or 11.
Example of the change in the eclipse.ini:
-vm
C:/Users/evand/Downloads/openjdk-11.0.2_windows-x64_bin/jdk-11.0.2/bin/javaw.exe
Your JAVA_HOME refers to the JRE, and JRE does not contain javac Java Compiler.
You need to point your JAVA_HOME environment variable to the JDK, i.e. the root directory of the Java binaries.
If your console prints the address of JDK 11 when you echo JAVA_HOME, then it might be that your IDE is configured to override that variable with the JRE directory when you're using that IDE.
Alternatively, your IDE can be using its bundled JRE, and your system might be referring to another Java binaries (JDK, in your case).

javac NotRecognized - Java version 1.8.0_161 |

I'm testing out some code on a computer running Windows 8.1.
I'm using the command prompt and I can't seem to get javac (or javaC) to compile my text file. I've added the path as an environment variable (...\bin) and it doesn't do work. java -version and java prints information to the console, just can't get javac to work.
Seems like you are having JRE installed on your machine. You can go to your JAVA_HOME/bin folder, check if javac file is there or not. If not, it means you have JRE installed. You need to install java JDK.
FOr download jdk, this is the url for JDK 8:
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Previous version available here:
http://www.oracle.com/technetwork/java/archive-139210.html

installed java 7 but terminal responds with version 6

So I installed Java 7 and verified that it was installed Java now shows up under System preferences. However when I run java -version it still returns
java version "1.6.0_65
How do i fix this so that my computer points to the correct version? Also, is this a problem in the future if i choose to not fix this?
Thanks
You need to set your java 7 directory bin path to system PATH variable. As you are using Mac OS, use this article to set path correctly:
JAVA HOME in Mac OS
Use the following command to list the installed versions of Java:
/usr/libexec/java_home -V
From the output of the above command, copy the path for the version of Java you want to use.
Open up your ~/.bash_profile or ~/.bashrc file and add/edit the line:
export JAVA_HOME=/Path/copied/from/step/2
In your terminal, reload the the file you edited in the previous step using the corresponding command below:
source ~/.bash_profile
source ~/.bashrc
Now, you should see the the correct version of Java on the command line:
java -version

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.
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
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".
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?
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

Java -version shows 1.3 when I updated the classpath to c:\java\jdk1.6.0_20\bin

I need to run a java -jar on command line and so installed jdk6 but whenever i do java -version in command line it still shows the old version (1.3) How do I update it to show the latest version ??
Add the new Java bin folder to your path. And remove any other java references from your path.
You should set the PATH environment variable. On the command prompt, do:
> set JAVA_HOME=c:\java\jdk1.6.0_20
> set PATH=%JAVA_HOME%\bin;%PATH%
Now doing java -version should give you the expected version information.
The JDK install does not update the default Java installation on the system.
Go to http://java.com/en/download/installed.jsp and use the suggested installation procedure there when it finds that your default Java is outdated.

Categories