如何在IntelliJ IDEA中启动Vert.x服务器?

35

我该如何在IntelliJ IDEA中启动一个简单的Vert.x服务器?

我的build.gradle文件如下:

apply plugin: 'java'

version = '3.0.0'

repositories {
    mavenCentral()
}

dependencies {
    compile 'io.vertx:vertx-core:3.0.0'
}

我的Vertx服务器,MyVertex.java如下:

package com.example;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;

public class MyVerticle extends AbstractVerticle {

    @Override
    public void start(Future<Void> fut) {
        vertx.createHttpServer()
                .requestHandler(r -> r.response().end("<h1>Hello</h1>"))
                .listen(8081);
    }
}

我的 IntelliJ 运行配置如下,主类为 io.vertx.core.Starter

enter image description here

但是当我使用这个运行配置运行时,会出现以下错误信息:

Error: Could not find or load main class run

运行配置中的 VM 选项run是否需要安装并添加到路径中,还是我该如何开始进行Vert.x服务器开发?


在“VM选项”中,不应该只是你的类名吗? - injecteer
@injecteer:然后我收到了这个错误信息:错误:找不到或加载主类com.example.MyVerticle - Jonas
在此页面上,它是作为VM选项运行<class>。https://github.com/vert-x3/vertx-examples/tree/master/gradle-verticles/gradle-verticle - Jonas
5个回答

54

我正在使用vertx 3.2.1,它对 io.vertx.core.Starter 感到不满。现在它已经被弃用了。因此,应该使用 io.vertx.core.Launcher

这是通过 intellij 启动并指定配置 JSON 文件选项的示例:

  • 主类: io.vertx.core.Launcher
  • VM 选项: <up to you, or leave blank>
  • 程序参数: run com.app.verticle.MyVerticle -conf /path/to/my_config.json

当使用日志框架时,将会在 VM 选项中添加如下内容。

Log4j 与 log4j 或 slf4j 委托:

-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4jLogDelegateFactory -Dlog4j.configuration=log4j.xml

-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory -Dlog4j.configuration=log4j.xml

Logback:

-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory -Dlogback.configurationFile=logback.xml

我尝试了-conf /path/my_config.json和-conf \path\my_config.json,但都没有起作用。我正在使用Windows。可能是什么问题? - MA1
@MoA - 当您指定完整路径到配置文件时会发生什么?例如:-conf c:/apps/conf/my_config.json ... - corindiano

32

只需将此代码添加到您的MyVerticle(或单独的类)中:

import io.vertx.core.Launcher;
...
public static void main(final String[] args) {
    Launcher.executeCommand("run", MyVerticle.class.getName());
}

然后只需按下Ctrl+Shift+F10来运行它,IntelliJ将自动创建Run Configuration


1
这更有用,因为如果您想将一个 Promise 传递给您的垂直启动方法。 - Chandan Kumar

8
啊,我的错误:
在IntelliJ IDEA的运行配置中,run com.example.MyVerticle 应该作为程序参数:的值而不是VM选项。请注意修改。

5

您可以简单地添加一个主程序并使用deployVerticle(),然后在IntelliJ中轻松运行或调试它。 使用deployVerticle,您可以传递一个新的主/引导垂直实例,或者您可以传递yourMainVerticle.class

public class VertxVerticleMain {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();

        vertx.deployVerticle(new MyVerticle());
       //vertx.deployVerticle(MyVerticle.class);

    }
}

2
要在IntelliJ IDEA中使用org.vertx.java.platform.impl.cli.Starter作为您的主类;如果您正在使用参数等内容,您可能需要使用像runmod<groupId>〜<artifactId>〜<version> [-conf src/main/resources/your_config.json -cp]这样的内容。
请查看此项目
对于Vert.x 3.0.0,您必须将io.vertx.core.Starter用作您的主类,并将run com.example.other.AnyVerticle用作您的程序参数。

2
io.vertx.core.Starter已在vert.x 3.2中被弃用,请使用io.vertx.core.Launcher代替。 - jwd630

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接