34

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

My build.gradle is as below:

apply plugin: 'java'

version = '3.0.0'

repositories {
    mavenCentral()
}

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

My Vertx-server, MyVertex.java is as below:

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

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) run something I need to install and add to my path or how do I get started with Vert.x-server development?

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • shouldn't in `VM Options` be just your class name? – injecteer Aug 25 '15 at 13:42
  • @injecteer: then I get this error message: `Error: Could not find or load main class com.example.MyVerticle` – Jonas Aug 25 '15 at 13:46
  • On this page it is "run " as VM Option https://github.com/vert-x3/vertx-examples/tree/master/gradle-verticles/gradle-verticle – Jonas Aug 25 '15 at 13:47

5 Answers5

52

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.

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

  • 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

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

Log4j with either log4j or slf4j delgate:

-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
ses
  • 13,174
  • 31
  • 123
  • 226
corindiano
  • 724
  • 7
  • 6
  • I tried -conf /path/my_config.json and -conf \path\my_config.json but didn't work. I'm using windows. What could be the problem? – MA1 Jul 06 '20 at 21:03
  • @MoA - what happens when you specify the complete path to conf? Example: -conf c:/apps/conf/my_config.json ... – corindiano Feb 10 '21 at 22:43
29

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

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

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

Jared
  • 571
  • 8
  • 8
8

Ah, my mistake:

run com.example.MyVerticle should be the value of Program arguments: and not as VM options in the IntelliJ IDEA Run configuration.

Jonas
  • 121,568
  • 97
  • 310
  • 388
5

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

public class VertxVerticleMain {

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

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

    }
}
RoundPi
  • 5,819
  • 7
  • 49
  • 75
2

You have to use this: org.vertx.java.platform.impl.cli.Starter as 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]

Have a look at this project.

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

x80486
  • 6,627
  • 5
  • 52
  • 111
  • 2
    `io.vertx.core.Starter` has been deprecated in vert.x 3.2; use `io.vertx.core.Launcher` instead. – jwd630 Feb 03 '16 at 22:38