问题
I have encountered a syntax problem in the following code used for Threading:
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
new Thread(() -> {
GrabberShowUsesCallable gs = new GrabberShowUsesCallable();
//GrabberShow gs = new GrabberShow();
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<String> future = executorService.submit(gs);
String cc;
try {
//Add data to table
cc = future.get();
model.addRow(new Object[] {row,0,cc,0});
row=row+1;
Thread.currentThread().stop();
} catch (InterruptedException | ExecutionException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}).start();
}
});
I got error at line 3 new thread:
Multiple markers at this line - Syntax error on token(s), misplaced construct(s) - Syntax error on tokens, delete these tokens
In that line I got two syntax error, one from (()
Syntax error on token(s), misplaced construct(s)
and one from -> {
Syntax error on tokens, delete these tokens
The code was running fine on 3 different laptops, except one (my laptop) encountered this problem. I am using Eclipse with jre 8.0 and jdk 8.0 installed.
回答1:
Make sure your java source level is java8 too, in the eclipse project settings overrides, if the eclipse default is not java8 source level. This is a typical overlook.
回答2:
If https://stackoverflow.com/a/50173565/139985 (setting the compiler source level) doesn't solve your problem, then here are a couple more things to check.
Lambda expressions are a Java 8+ feature, so:
- Check that your JDK / JRE is Java 8 or later.
- Check that you are using a version of Eclipse that supports Java 8. The first major release of Eclipse to support Java 8 "out of the box" was Eclipse Luna (R 4.4).
Also, if you are using Maven, make sure that the Maven POM file explicitly specifies the Java source level:
- Set default java compliance level for maven projects in eclipse
The default source for Maven is Java 5, and this will clobber the source level you set in the Eclipse settings for your project.
来源:https://stackoverflow.com/questions/50169747/syntax-error-on-tokens-misplaced-constructs-for-lambda-expression