简体   繁体   English

标记上的语法错误,Lambda 表达式错位的构造

[英]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:我在第 3 行新线程中遇到错误:

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.代码在 3 台不同的笔记本电脑上运行良好,除了一台(我的笔记本电脑)遇到了这个问题。 I am using Eclipse with jre 8.0 and jdk 8.0 installed.我正在使用安装了 jre 8.0 和 jdk 8.0 的 Eclipse。

Make sure your java source level is java8 too, in the eclipse project settings overrides, if the eclipse default is not java8 source level.确保您的java源代码级别也是java8,在eclipse项目设置覆盖中,如果eclipse默认不是java8源代码级别。 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.如果https://stackoverflow.com/a/50173565/139985 (设置编译器源代码级别)不能解决您的问题,那么这里还有几件事需要检查。

Lambda expressions are a Java 8+ feature, so: Lambda 表达式是 Java 8+ 的一项功能,因此:

  • Check that your JDK / JRE is Java 8 or later.检查您的 JDK / JRE 是否为 Java 8 或更高版本。
  • 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) .检查您使用的是支持 Java 8 的 Eclipse 版本。Eclipse 的第一个“开箱即用”支持 Java 8 的主要版本是Eclipse Luna (R 4.4)

Also, if you are using Maven, make sure that the Maven POM file explicitly specifies the Java source level:此外,如果您使用 Maven,请确保 Maven POM 文件明确指定 Java 源代码级别:

The default source for Maven is Java 5, and this will clobber the source level you set in the Eclipse settings for your project. Maven 的默认源代码是 Java 5,这将破坏您在 Eclipse 设置中为项目设置的源代码级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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