静态函数作为参数(::)抛出错误

时间:2015-05-15 15:48:00

标签: java gradle

使用jenetics遗传算法库,但我几乎肯定它独立于问题。似乎Java不理解Java 8中引入的双冒号表达式.Java 8已安装且Gradle正在使用它。

由于以下类中的双冒号表达式,构建失败:

package gen;

import org.jenetics.BitChromosome;
import org.jenetics.BitGene;
import org.jenetics.Genotype;
import org.jenetics.engine.Engine;
import org.jenetics.engine.EvolutionResult;

public class HelloWorld {
    // 2.) Definition of the fitness function.
    private static Integer eval(Genotype<BitGene> gt) {
        return ((BitChromosome)gt.getChromosome()).bitCount();
    }

    public static void main(String[] args) {
        // 1.) Define the genotype (factory) suitable
        //     for the problem.
        Factory<Genotype<BitGene>> gtf = Genotype.of(BitChromosome.of(10, 0.5));

        // 3.) Create the execution environment.
        Engine<BitGene, Integer> engine = Engine.builder(HelloWorld::eval, gtf).build();

        // 4.) Start the execution (evolution) and
        //     collect the result.
        // Genotype<BitGene> result = engine.stream().limit(100).collect(EvolutionResult.toBestGenotype());

        // System.out.println("Hello World:\n" + result);
    }
}

提供Gradle输出:

russell@Ubuntu-LTS:~/workspace/react_gen $ ./gradlew build
:compileJava
/home/russell/workspace/react_gen/src/main/java/gen/HelloWorld.java:21: error: ')' expected
        Engine<BitGene, Integer> engine = Engine.builder(HelloWorld::main, gtf).build();
                                                                   ^
/home/russell/workspace/react_gen/src/main/java/gen/HelloWorld.java:21: error: illegal start of expression
        Engine<BitGene, Integer> engine = Engine.builder(HelloWorld::main, gtf).build();
                                                                    ^
/home/russell/workspace/react_gen/src/main/java/gen/HelloWorld.java:21: error: ';' expected
        Engine<BitGene, Integer> engine = Engine.builder(HelloWorld::main, gtf).build();
                                                                              ^
3 errors
:compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.077 secs

1 个答案:

答案 0 :(得分:3)

我需要添加:

compileJava {
    sourceCompatibility: '1.8'
    targetCompatibility: '1.8'
}

在我的build.gradle中,apply plugin: 'java'之后

Source