Syntax error on token(s), misplaced construct(s) for lambda expression

社会主义新天地 提交于 2021-02-16 21:17:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!