Java 将asynchttpclient作为maven项目发送异步post请求并运行

Java 将asynchttpclient作为maven项目发送异步post请求并运行,java,eclipse,maven,Java,Eclipse,Maven,我在这里完全疯了。这是我第一次使用java,它让我非常害怕 无论如何,我需要做的是创建一个小型java maven项目,在其中我监听一个特定的UDP端口(python脚本atm,模拟我应该使用的设备),然后在发送一个post请求到一个带有两个post参数的特定url之前做一些事情 我发现这个图书馆似乎得到了很好的评价和声誉 到目前为止我完成了什么 我已经成功创建了一个maven项目(显然) 编写了receiver类(工作得很好!)——该类捕获UDP消息并存储任何需要存储的内容,以便在以后发送PO

我在这里完全疯了。这是我第一次使用java,它让我非常害怕

无论如何,我需要做的是创建一个小型java maven项目,在其中我监听一个特定的UDP端口(python脚本atm,模拟我应该使用的设备),然后在发送一个post请求到一个带有两个post参数的特定url之前做一些事情

我发现这个图书馆似乎得到了很好的评价和声誉

到目前为止我完成了什么

  • 我已经成功创建了一个maven项目(显然)
  • 编写了receiver类(工作得很好!)——该类捕获UDP消息并存储任何需要存储的内容,以便在以后发送POST消息时使用
  • 编写了负责使用库发送POST消息的代码(我不确定它是否有效,因为我无法运行它):
  • 尝试通过powershell运行以下操作:

  • 如果您能在这方面给予帮助,我们将不胜感激。乐意在需要时提供更多信息。

    您还需要在类路径中包含http库。现在它只包含您的jar。您还需要将依赖项添加到cp,以便它可以找到它。maven可以为您实现这一点,但是使用java运行它时,您需要做一些明确的事情,您可以使用maven创建一个可执行的jar。你可以找到这个有用的@HenningLuther我不确定你的意思,你能给我指点方向吗?你正在配置的类路径-cp只包含你自己的工件,但不包含对http的依赖,你也必须添加它。maven将pom中定义的所有依赖项添加到类路径中,这就是它使用maven的原因。
        // To make post request to send data
      AsyncHttpClient client = Dsl.asyncHttpClient();
    
      BoundRequestBuilder postRequest = client
              .preparePost("http://localhost:8080/my_custom_url/")
              .addFormParam("id", myid)
              .addFormParam("stream", inData);
    
      // Asynchronous execution
      postRequest.execute(new AsyncHandler<Object>() {
          public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
              System.out.print("\nstatus received");
              return null;
          }
    
          public State onHeadersReceived(HttpHeaders headers) throws Exception {
              System.out.print("\nheaders received");
              return null;
          }
    
          public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
              System.out.print("\nbody part received");
              return null;
          }
    
          public void onThrowable(Throwable t) {
    
          }
    
          public Object onCompleted() throws Exception {
              System.out.print("\nexecute completed");
              return null;
          }
      });
    
      // Close to stop memory leaks after we are done
      try {           client.close();         } catch (IOException e) {           infoBox("Could not close AsyncHttpClient.", "Error");
          System.exit(0);         }
    
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building MyProject 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MyProject ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory C:\Users\me\eclipse-workspace\MyProject\src\main\resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MyProject ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MyProject ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory C:\Users\me\eclipse-workspace\MyProject\src\test\resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MyProject ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO] 
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MyProject ---
    [INFO] Surefire report directory: C:\Users\me\eclipse-workspace\MyProject\target\surefire-reports
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running me.company.MyProject.AppTest
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec
    Results :
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    [INFO] 
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MyProject ---
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1.044 s
    [INFO] Finished at: 2018-05-21T13:23:49+03:00
    [INFO] Final Memory: 10M/245M
    [INFO] ------------------------------------------------------------------------
    
    PS C:\Users\me\eclipse-workspace\MyProject\target> java -cp .\MyProject-0.0.1-SNAPSHOT.jar me.company.MyProject.App
    Error: A JNI error has occurred, please check your installation and try again
    Exception in thread "main" java.lang.NoClassDefFoundError: org/asynchttpclient/AsyncHandler
            at java.lang.Class.getDeclaredMethods0(Native Method)
            at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
            at java.lang.Class.privateGetMethodRecursive(Unknown Source)
            at java.lang.Class.getMethod0(Unknown Source)
            at java.lang.Class.getMethod(Unknown Source)
            at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
            at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: org.asynchttpclient.AsyncHandler
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            ... 7 more