Java 如何从 IntelliJ IDEA 启动 Vert.x 服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32205477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:09:24  来源:igfitidea点击:

How to start Vert.x server from IntelliJ IDEA?

javaintellij-ideagradlevert.x

提问by Jonas

How do I start a simple Vert.x server from inside IntelliJ IDEA?

如何从 IntelliJ IDEA 内部启动一个简单的 Vert.x 服务器?

My build.gradleis as below:

build.gradle的如下:

apply plugin: 'java'

version = '3.0.0'

repositories {
    mavenCentral()
}

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

My Vertx-server, MyVertex.javais as below:

我的 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);
    }
}

And my IntelliJ run configuration is as below, with io.vertx.core.Starteras main class: enter image description here

我的 IntelliJ 运行配置如下,io.vertx.core.Starter作为主类: 在此处输入图片说明

But when I run it with my run configuration I get this error message:

但是当我使用运行配置运行它时,我收到以下错误消息:

Error: Could not find or load main class run

Is the VM option (in Run configuration) runsomething I need to install and add to my path or how do I get started with Vert.x-server development?

VM 选项(在运行配置中)run是否需要安装并添加到我的路径中,或者我如何开始使用 Vert.x-server 开发?

采纳答案by corindiano

I'm using vertx 3.2.1 and it's complaining about io.vertx.core.Starter. It's deprecated now. So, one should use io.vertx.core.Launcher.

我正在使用 vertx 3.2.1 并且它在抱怨io.vertx.core.Starter. 现在已弃用。因此,应该使用io.vertx.core.Launcher.

This is an example of launching via intellij with the option of specifying a config JSON file:

这是通过 Intellij 启动的示例,可选择指定配置 JSON 文件:

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

When using a logging framework it will be added in VM Options as below.

使用日志框架时,它将添加到 VM 选项中,如下所示。

Log4j with either log4j or slf4j delgate:

带有 log4j 或 slf4j delgate 的 Log4j:

-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

回答by Jonas

Ah, my mistake:

啊,我的错误:

run com.example.MyVerticleshould be the value of Program arguments:and not as VM optionsin the IntelliJ IDEA Run configuration.

run com.example.MyVerticle应该是Program arguments:的值,而不是IntelliJ IDEA Run 配置中的VM 选项

回答by x80486

You have to use this: org.vertx.java.platform.impl.cli.Starteras your Main Class in IntelliJ IDEA; and if you are using arguments and things like that you might want to use something like: runmod <groupId>~<artifactId>~<version> [-conf src/main/resources/your_config.json -cp]

你必须使用这个:org.vertx.java.platform.impl.cli.Starter作为你在 IntelliJ IDEA 中的主类;如果您正在使用参数和类似的东西,您可能想要使用以下内容:runmod <groupId>~<artifactId>~<version> [-conf src/main/resources/your_config.json -cp]

Have a look at this project.

看看这个项目

For Vert.x 3.0.0 you have to use this: io.vertx.core.Starteras your Main Class and run com.example.other.AnyVerticleas your Program arguments.

对于 Vert.x 3.0.0,你必须使用这个:io.vertx.core.Starter作为你的主类和run com.example.other.AnyVerticle你的程序参数。

回答by Jared Weinfurtner

Simply add this to your MyVerticle(or a separate class):

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

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

Then simply Ctrl+Shift+F10to run it and IntelliJ will automatically create the Run Configuration.

然后只需Ctrl+Shift+F10运行它,IntelliJ 就会自动创建Run Configuration.

回答by Gob00st

You can simply add a main and use deployVerticle() and then from there in IntelliJ you can Run or Debug it easily. With deployVerticle, you can pass a new instance of your main/bootstrap verticle or you can pass yourMainVerticle.class

您可以简单地添加一个 main 并使用 deployVerticle() 然后从那里在 IntelliJ 中您可以轻松地运行或调试它。使用 deployVerticle,您可以传递 main/bootstrap verticle 的新实例,也可以传递 yourMainVerticle.class

public class VertxVerticleMain {

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

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

    }
}