简体   繁体   English

如何使用命令行编译和启动Java Undertow HTTP服务器

[英]how to compile and start a Java undertow HTTP server using command line

I have the following project folder setup for a simple Java undertow HTTP server: 我为简单的Java undertow HTTP服务器设置了以下项目文件夹:

- HelloWorldServer.java

- build

- lib

I downloaded XNIO and JBOSS-Logging into lib. 我将XNIO和JBOSS-Logging下载到lib中。 I made sure I downloaded the right distributions by letting Gradle download the dependencies. 我通过让Gradle下载依赖项来确保下载了正确的发行版。 But I'm trying to understand more by running things manually so I copy the jar files into the lib folder. 但是我试图通过手动运行来了解更多信息,因此我将jar文件复制到lib文件夹中。

My HelloWorldServer.java looks like this: 我的HelloWorldServer.java看起来像这样:

import io.undertow.Undertow;
import io.undertow.util.*;


public class HelloWorldServer {
public static void main(final String[] args) {
    Undertow server = Undertow.builder()
                              .addHttpListener(8080, "localhost")
                              .setHandler(exchange -> {
                                  exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                                  exchange.getResponseSender().send("Hello World");
                              }).build();
    server.start();
    }
}

Then I go to my folder root and run the following command: 然后,转到文件夹根目录并运行以下命令:

javac -cp "lib/*" -d build HelloWorldServer.java

Now that I've compiled the application, how do I run it from terminal? 既然我已经编译了应用程序,如何从终端运行它?

EDIT: the content of my lib directory is as follows: 编辑:我的lib目录的内容如下:

hamcrest-core-1.3.jar // i believe thats not needed, but leave it anyway
jboss-logging-3.2.1.Final.jar
undertow-core-2.0.0.Alpha1.jar
xnio-api-3.3.6.Final.jar
xnio-nio-3.3.6.Final.jar

由于使用-d标志,您要求Java编译器在build目录中生成类文件,因此需要像这样运行:

java -cp "build/:lib/*" HelloWorldServer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM