Sounds like your code is not compiled under JDK 7. Check the version of your compiler (as Sotirios mentioned)
java 7 features not working on ubuntu with java version "1.7.0_51"
-
04-07-2023 - |
Domanda
According to java -version
this is what my Ubuntu Java environment is like:
java version "1.7.0_51"
OpenJDK Runtime Environment (IcedTea 2.4.4) (7u51-2.4.4-0ubuntu0.12.04.2)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
My javac -version
is:
javac 1.6.0_30
How do I change my javac version? Thanks for the tip @SotiriosDelimanolis. ;) (If you are reading this and have the same problem read the comments. I link to a page that describes how to do this on ubuntu).
I am trying to run the following program:
import java.util.*;
class Separate {
public static void main(String[] args) {
String text = "<head>first program</head> <body>hello world</body>";
Set<String> words = new TreeSet<>(); //1 Compiler error
try(Scanner tokenizingScanner = new Scanner(text)) { //2 Compiler Error
tokenizingScanner.useDelimeter("\\W");
while(tokenizingScanner.hasNext()) {
String word = tokenizingScanner.next();
if(!word.trim().equals("")) {
words.add(word);
}
} //end while
} //end try
for(String word: words) {
System.out.print(word + " ");
} //end for
}
I receive these errors upon trying to compile:
Separate.java:8: illegal start of type
Set<String> words = new TreeSet<>();
^
Separate.java:9: '{' expected
try(Scanner tokenizingScanner = new Scanner(text)) {
^
Separate.java:9: ')' expected
try(Scanner tokenizingScanner = new Scanner(text)) {
^
Separate.java:9: ';' expected
try(Scanner tokenizingScanner = new Scanner(text)) {
^
Separate.java:9: 'try' without 'catch' or 'finally'
try(Scanner tokenizingScanner = new Scanner(text)) {
^
Separate.java:24: reached end of file while parsing
}
^
6 errors
These errors seem like they should not be errors. The first error is showing that the diamond notation found in Java 7 is not correct syntax or something when it is correct. This error is shown with 1 above in the comments.
The other errors stemming from the Scanner object creation in the try block is a try with resources that is also a Java 7 feature. This line is marked with 2 above in the source code.
Does anyone know what I am missing?
Soluzione 2
Altri suggerimenti
Check your PATH environment variable, seems like it is pointing to an older JDK.
You can check to see which javac is being used by using
which javac
You can directly call the new JDK by using it's full path e.g.
/usr/java/jdk1.7.0_51/bin/javac
But if you want to call javac anywhere you will have to change the PATH environment variable to point to the newer JDK.
How you modify your PATH depends on your setup. You'll have to remove the older path from PATH that points to JDK1.6 and add in a new path that points to JDK1.7
Example:
If
which javac
produces
/usr/java/jdk1.6.0_30/bin/javac
Then your path might look something like this, notice the jdk1.6.0_30
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/java/jdk1.6.0_30/bin
Change that to the proper path (jdk1.7.0_51)
PATH=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/java/jdk1.7.0_51/bin
Useful links: