使用Vert.x创建路由时遇Address already in use问题求助
Hey there! Let's work through that "Address already in use" error you're hitting with your Vert.x routing setup. I'll break down the most likely causes and how to fix them step by step:
1. The port you're trying to use is occupied by another process
This is the most common culprit. The port you're binding (like the default 8080) might already be in use by another application—think another Vert.x instance, Tomcat, Nginx, or even a random background service.
How to check:
- Windows: Open Command Prompt and run:
netstat -ano | findstr :<your-port-number>
Note the PID of the process using the port, then end it via Task Manager. - Linux/macOS: Open Terminal and run either:
lsof -i :<your-port-number>
ornetstat -tulpn | grep :<your-port-number>
Kill the process withkill <PID>.
Quick fix without killing other processes:
Just switch to an unused port when starting your HTTP server. Here's how to update your code:
@Override public void start() throws Exception { Router router = Router.router(vertx); router.route().handler(routingContext -> { routingContext.response() .putHeader("content-type","text/html;charset=UTF-8") .end("peopl..."); }); // Use port 8081 instead of the occupied one vertx.createHttpServer().requestHandler(router).listen(8081); }
2. You're redeploying the same Verticle or creating multiple HTTP servers
Looking at your code, you've set up the Router but haven't started an HTTP server yet. But if your launch logic (like a main method) is deploying the VerticleMain instance multiple times, or if you accidentally call listen() on multiple HTTP servers bound to the same port, you'll get this error.
Example of bad code:
// ❌ Don't do this! Duplicate deployment causes port conflict public static void main(String[] args) { Vertx vertx = Vertx.vertx(); vertx.deployVerticle(new VerticleMain()); vertx.deployVerticle(new VerticleMain()); // Second deployment tries to bind the same port }
Fix:
- Make sure you only deploy your
VerticleMainonce in your launch code. - Double-check that you're not calling
vertx.createHttpServer().listen()more than once in your verticle.
3. Previous Vert.x instance didn't shut down properly, leaving the port hanging
If you force-closed your Vert.x app (like hitting Ctrl+C abruptly), the OS might not release the port right away. This is a common dev environment issue.
Fixes:
- Wait 1-2 minutes for the OS to automatically release the port.
- Or enable port reuse (only for development—not recommended for production):
public static void main(String[] args) { VertxOptions options = new VertxOptions(); options.getEventLoopPoolOptions().setReusePort(true); Vertx vertx = Vertx.vertx(options); vertx.deployVerticle(new VerticleMain()); }
One last note:
Your current code is missing the HTTP server startup logic—even without the port error, it won't handle any incoming requests. Make sure you add the vertx.createHttpServer().requestHandler(router).listen(...) line as shown in the first fix!
内容的提问来源于stack exchange,提问作者Abbott Liu

