我们运行Java程序,经常会遇到端口号被占用的情况。
关闭的命令如下:
1.查找进程号(如查找8903端口对应的进程号)
netstat -ano | findstr 8903
2.根据进程号杀掉端口(如杀掉进程号为42988的进程)
taskkill /f /pid 42988
执行效果如下图
批处理命令为
@echo off & setlocal EnableDelayedExpansion
chcp 65001
title ClosePort
set /p port="Please enter the port number:"
set pid=0
for /f "tokens=2,5" %%b in ('netstat -ano ^| findstr ":%port%"') do (
set temp=%%b
for /f "usebackq delims=: tokens=1,2" %%i in (`set temp`) do (
if %%j==%port% (
taskkill /f /pid %%c
set pid=%%c
echo Port [%port%] the process has been shut down
) else (
echo Port not occupied by the local machine [%port%]
)
)
)
if !pid!==0 (
echo Port [%port%] not occupied
)
echo Operation successful
pause
另存为时选择ANSI编码。
ANSI编码用中文会乱码,所以里面都使用英文。