运行Vert.x应用时遭遇ClassNotFoundException问题求助
Hey there, let's work through that ClassNotFoundException you're hitting with your Vert.x application. First, let's recap your setup to make sure we're aligned:
Your Verticle Code
public class MyFirstVerticle extends AbstractVerticle { @Override public void start(Future<Void> fut) { vertx .createHttpServer() .requestHandler(r -> { r.response().end("<h1>Hello from my first " + "Vert.x 3 application</h1>"); }) .listen(8080, result -> { if (result.succeeded()) { fut.complete(); } else { fut.fail(result.cause()); } }); } }
Incomplete Maven POM
You shared a partial POM starting with:
<project xmlns="http://maven.apache.org..."
Now let's break down the most likely causes and fixes:
1. Missing or Incorrect Vert.x Dependencies
The top culprit here is almost always missing core Vert.x libraries in your POM. The AbstractVerticle and Vertx classes your code relies on live in the vertx-core artifact. Add this full dependency block inside your POM's <dependencies> section:
<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</artifactId> <version>3.9.16</version> <!-- Use the latest Vert.x 3 version, or adjust to your intended version --> </dependency>
Note: If you're targeting Vert.x 4 instead, use a 4.x version number like 4.5.1
2. Classpath Misconfiguration When Running
Even with correct dependencies, you might be running the app without including compiled classes or required JARs. Use Maven's exec plugin to ensure the classpath is properly set up:
- Add this plugin to your POM's
<build><plugins>section:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <configuration> <mainClass>com.yourpackage.MyFirstVerticle</mainClass> <!-- Replace with your actual full class name (including package!) --> </configuration> </plugin>
- Run the app with this command to ensure clean compilation and correct execution:
mvn clean compile exec:java
3. Missing Package Declaration
Your code snippet doesn't include a package statement. If you added one in your actual file (e.g., package com.myapp;), make sure you're using the full qualified class name when running (not just MyFirstVerticle). If you're using the default package (not recommended for production), set the exec plugin's mainClass to just MyFirstVerticle.
4. Verify Compiled Classes
Double-check that Maven is generating the class file correctly. Look in the target/classes folder—you should see MyFirstVerticle.class (or in a subfolder if you're using a package). If it's missing, run mvn clean compile to rebuild your project.
Let me know if any of these steps resolve your issue!
内容的提问来源于stack exchange,提问作者KRR16

