如何使用Java 8运行Vertx

时间:2014-06-17 16:02:53

标签: java intellij-idea java-8 vert.x intellij-13

我已下载并运行Vertx,但当我编辑其中一个示例以开始使用Java 8功能(lambdas)时,我一直遇到编译器错误。在Intellij中,一切都编译得很好,我有IntelliJ指向我的Java 8 JDK,但是当我从命令行运行Vertx时,会出现编译错误:

> vertx run FanoutServer.java
C:\Users\i50640\Documents\Intellij Projects\EchoServer\src\main\java\echoserver\FanoutServer.java:33: error: illegal start of expression
        vertx.createNetServer().connectHandler(socket -> {
                                                       ^
C:\Users\i50640\Documents\Intellij Projects\EchoServer\src\main\java\echoserver\FanoutServer.java:33: error: illegal start of expression
        vertx.createNetServer().connectHandler(socket -> {
                                                         ^
C:\Users\i50640\Documents\Intellij Projects\EchoServer\src\main\java\echoserver\FanoutServer.java:33: error: ';' expected
        vertx.createNetServer().connectHandler(socket -> {
                                                          ^
C:\Users\i50640\Documents\Intellij Projects\EchoServer\src\main\java\echoserver\FanoutServer.java:34: error: illegal start of expression
            connections.add(socket.writeHandlerID());
Failed in deploying verticle
java.lang.RuntimeException: Compilation failed
    at org.vertx.java.platform.impl.java.CompilingClassLoader.<init>(CompilingClassLoader.java:108)
    at org.vertx.java.platform.impl.java.JavaVerticleFactory.createVerticle(JavaVerticleFactory.java:51)
    at org.vertx.java.platform.impl.DefaultPlatformManager$21.run(DefaultPlatformManager.java:1723)
    at org.vertx.java.core.impl.DefaultContext$3.run(DefaultContext.java:175)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:370)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:353)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.RuntimeException: Compilation failed!
    at org.vertx.java.platform.impl.java.CompilingClassLoader.<init>(CompilingClassLoader.java:105)
    ... 7 more

Failed in deploying verticle
java.lang.RuntimeException: Compilation failed
    at org.vertx.java.platform.impl.java.CompilingClassLoader.<init>(CompilingClassLoader.java:108)
    at org.vertx.java.platform.impl.java.JavaVerticleFactory.createVerticle(JavaVerticleFactory.java:51)
    at org.vertx.java.platform.impl.DefaultPlatformManager$21.run(DefaultPlatformManager.java:1723)
    at org.vertx.java.core.impl.DefaultContext$3.run(DefaultContext.java:175)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:370)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:353)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.RuntimeException: Compilation failed!
    at org.vertx.java.platform.impl.java.CompilingClassLoader.<init>(CompilingClassLoader.java:105)
    ... 7 more

这是FanoutServer.java文件:

public class FanoutServer extends Verticle {

    public void start()  {
        final Set<String> connections = vertx.sharedData().getSet("conns");

        vertx.createNetServer().connectHandler(socket -> {
            connections.add(socket.writeHandlerID());
            socket.dataHandler(buffer -> {
                for (String actorID : connections) {
                    vertx.eventBus().publish(actorID, buffer);
                }
            });
            socket.closeHandler(new VoidHandler() {
                public void handle() {
                    connections.remove(socket.writeHandlerID());
                }
            });
        }).listen(6748);
    }
}

在能够使用Java 8功能之前,是否需要下载Vertx源代码并在Java 8中编译它?如果没有,我需要做些什么才能使其正常工作?

2 个答案:

答案 0 :(得分:3)

检查您的JAVA_HOME环境变量,并确保它也指向右侧jdk

答案 1 :(得分:2)

答案可以在Vertx的google小组中找到:

https://groups.google.com/forum/#!topic/vertx/pG7mIo3-03o

简而言之,我的Windows机器上的JAVA_HOME变量指向JDK 1.7而不是1.8。