java bat脚本集合,后台运行jar脚本,防止多次双击启动脚本,结束某个端口脚本

后台运行jar bat脚本:

@echo off
IF "%JAVA_HOME%" == "" (
    echo Please set environment variable JAVA_HOME!
    @pause
    exit
)
setlocal & pushd
setlocal EnableDelayedExpansion
set MAIN_CLASS=com.ude.hl7v3.client.Hl7v3ClientRzApplication
set APP_BASE_PATH=%~dp0
set CP=%APP_BASE_PATH%\hl7v3-client-rz\config;%APP_BASE_PATH%\hl7v3-client-rz\lib\*
set port=8000
for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port%"') do (
   taskkill /pid %%m -t -f
   ping -n 3 127.0.0.1 > nul
   echo application is running, the port is %%m
 goto q
)
:q
netstat -ano |findstr "LISTENING" |findstr ":%port%" > nul
::echo %errorlevel%
if %errorlevel% equ 0 (
    goto open
) else (
    goto run
)
:run  - start jar
start javaw -Xmx512m -Xms256m -Dfile.encoding=UTF-8 -Xverify:none -cp %CP% %MAIN_CLASS% &
@echo Starting the application, Please wait 10s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 9s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 8s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 7s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 6s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 5s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 4s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 3s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 2s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 1s...
ping -n 2 127.0.0.1 > nul
start chrome "http://127.0.0.1:%port%"
@echo Please visit http://127.0.0.1:%port%
endlocal & popd
exit
:open  - open jar
echo "application is running"
start chrome "http://127.0.0.1:%port%"
@echo Please visit http://127.0.0.1:%port%
endlocal & popd
exit

防止多次双击重复启动脚本:

@echo off
IF "%JAVA_HOME%" == "" (
    echo Please set environment variable JAVA_HOME!
    @pause
    exit
)
setlocal & pushd
setlocal EnableDelayedExpansion
set MAIN_CLASS=com.ude.hl7v3.client.Hl7v3ClientRzApplication
set APP_BASE_PATH=%~dp0
set CP=%APP_BASE_PATH%\hl7v3-client-rz\config;%APP_BASE_PATH%\hl7v3-client-rz\lib\*
set port=8000
netstat -ano |findstr "LISTENING" |findstr ":%port%" > nul
::echo %errorlevel%
if %errorlevel% equ 0 (
    goto open
) else (
    goto run
)
:run  - start jar
start javaw -Xmx512m -Xms256m -Dfile.encoding=UTF-8 -Xverify:none -cp %CP% %MAIN_CLASS% &
@echo Starting the application, Please wait 10s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 9s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 8s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 7s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 6s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 5s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 4s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 3s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 2s...
ping -n 2 127.0.0.1 > nul
@echo Starting the application, Please wait 1s...
ping -n 2 127.0.0.1 > nul
start chrome "http://127.0.0.1:%port%"
@echo Please visit http://127.0.0.1:%port%
endlocal & popd
exit
:open  - open jar
echo "application is running"
start chrome "http://127.0.0.1:%port%"
@echo Please visit http://127.0.0.1:%port%
endlocal & popd
exit

安装某个exe的jar程序脚本:

@echo off

IF "%JAVA_HOME%" == "" (
    echo Please set environment variable JAVA_HOME!
    @pause
    exit
)

set JAVAW_LAUNCHER=javaw-antInstaller.exe
set JAR_FILE=ant-installer-0.0.1-SNAPSHOT.jar
set web_port=16888

tasklist | find /i "%JAVAW_LAUNCHER%" > nul

if %ErrorLevel% equ 0 (
    :: 关闭启动的antInstaller
    echo Alreay launching antInstaller, kill process ...
    taskkill -f -t -im %JAVAW_LAUNCHER%
    ping -n 1 127.0.0.1 > nul
    del %JAVAW_LAUNCHER%
    echo Kill antInstaller process ... done
)

copy "%JAVA_HOME%\bin\javaw.exe" %JAVAW_LAUNCHER%

start "javaw" %JAVAW_LAUNCHER% -jar %JAR_FILE% --server.port=%web_port%

@echo Starting the Ant-Installer, Please wait 10s...
ping -n 2 127.0.0.1 > nul
@echo Starting the Ant-Installer, Please wait 9s...
ping -n 2 127.0.0.1 > nul
@echo Starting the Ant-Installer, Please wait 8s...
ping -n 2 127.0.0.1 > nul
@echo Starting the Ant-Installer, Please wait 7s...
ping -n 2 127.0.0.1 > nul
@echo Starting the Ant-Installer, Please wait 6s...
ping -n 2 127.0.0.1 > nul
@echo Starting the Ant-Installer, Please wait 5s...
ping -n 2 127.0.0.1 > nul
@echo Starting the Ant-Installer, Please wait 4s...
ping -n 2 127.0.0.1 > nul
@echo Starting the Ant-Installer, Please wait 3s...
ping -n 2 127.0.0.1 > nul
@echo Starting the Ant-Installer, Please wait 2s...
ping -n 2 127.0.0.1 > nul
@echo Starting the Ant-Installer, Please wait 1s...
ping -n 2 127.0.0.1 > nul

::start chrome "http://127.0.0.1:%web_port%"

@echo Please visit http://127.0.0.1:%web_port%

@pause

启动某个exe的jar脚本:

@echo off

cd %~dp0

IF "%JAVA_HOME%" == "" (
    echo Please set environment variable JAVA_HOME!
    @pause
    exit
)

set PLUGIN_DIR=plugins
set APPLICATION_NAME=web-management
set JAVAW_LAUNCHER=javaw-web-management.exe
set JAR_FILE=web-management-4.8.1.jar

setlocal EnableDelayedExpansion

for %%a in (..\web-management-*.jar) do set JAR_FILE=%%~fa

set PROFILE_EVN=prod
::set JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -server -Dfile.encoding=UTF-8 -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Xloggc:../logs/gc.log"
set JAVA_OPTS=-Dfile.encoding=UTF-8 -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Xloggc:logs/gc.log

tasklist | find /i "%JAVAW_LAUNCHER%" > nul

if %ErrorLevel% equ 0 (
    :: 关闭启动的应用
    echo Alreay launching %APPLICATION_NAME%, kill process ...
    taskkill -f -t -im %JAVAW_LAUNCHER%
    ping -n 1 127.0.0.1 > nul
    del %JAVAW_LAUNCHER%
    echo Kill %JAVAW_LAUNCHER% process ... done
)

copy "%JAVA_HOME%\bin\javaw.exe" %JAVAW_LAUNCHER%

cd ..

start "javaw" bin/%JAVAW_LAUNCHER% %JAVA_OPTS% -Dloader.path=%PLUGIN_DIR% -jar -Dspring.profiles.active=%PROFILE_EVN% %JAR_FILE% --spring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,./config/application-prod.yml

exit

结束exe程序脚本:

@echo off

cd %~dp0

set JAVAW_LAUNCHER=javaw-web-management.exe

tasklist | find /i "%JAVAW_LAUNCHER%" > nul

if %ErrorLevel% equ 0 (
    :: 关闭启动的应用
    echo Alreay launching %APPLICATION_NAME%, kill process ...
    taskkill -f -t -im %JAVAW_LAUNCHER%
    ping -n 1 127.0.0.1 > nul
    del %JAVAW_LAUNCHER%
    echo Kill %JAVAW_LAUNCHER% process ... done
)

exit

结束某个端口脚本:

@echo off
set port=8000
for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port%"') do (
 echo kill the process %%m who use the port 
 taskkill /pid %%m -t -f
 goto q
)
:q

 

扫码领红包

微信赞赏支付宝扫码领红包

发表回复

后才能评论