简体   繁体   中英

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

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.

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.

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:

The default source for Maven is Java 5, and this will clobber the source level you set in the Eclipse settings for your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM