前提:
脚本假设需要给脚本传递两个参数,参数最终传给java程序
脚本同级目录中classes内存放的是程序运行的classes文件
脚本同级目录中lib内存放的是程序运行的jar包
非最佳脚本,根据需求自行修改
启动脚本:
不需要传递参数直接双击打开或者命令行输入脚本名称运行即可
此脚本假设和需要传递两个参数,需要在命令上输入路径\脚本名称 参数一 参数二
脚本内容:
::
::Author:wangxueming
::Date:2018-09-27
::
:: "::" annotation
:: "@echo off" only show results, no command line is displayed
:: "echo off" only show results, no command line is displayed, but this command line is included
@echo off
title Program
:: start delayed expansion to use variable nesting eg. ! %%! and Loop assignment
setlocal enabledelayedexpansion
cls
:: 获取参数,此脚本假设有两个参数
set arg1=%1%
set arg2=%2%
::统计参数个数
set /a cnt=0
:loop
if not "%1"=="" (set /a cnt+=1&shift /1&goto :loop)
if %cnt% NEQ 2 (
echo Usage:need two args.
goto exit
)
::JAVA_HOME目录
::set JAVA_HOME=/usr/java/jdk1.7.0_79
::Java应用根目录
set APP_HOME=%cd%
::需要启动的Java类
set APP_MAINCLASS=com.dcits.startup.Bootstrap
::classpath参数,包括指定lib目录下所有的jar
set CLASSPATH=%APP_HOME%\classes
For /r "%APP_HOME%\lib" %%f in (*.jar) do (
set CLASSPATH=!CLASSPATH!;%%f
)
::java虚拟机启动参数, MaxPermSize=128m; support was removed in 8.0
set JAVA_OPTS=-Xms512m -Xmx512m -Xmn256m -Djava.awt.headless=true -XX:MaxPermSize=128m
::打印环境信息
echo System Information:
echo ********************************************************
echo COMPUTERNAME=%COMPUTERNAME%
echo OS=%OS%
echo.
echo APP_HOME=%APP_HOME%
echo JAVA_HOME=%JAVA_HOME%
echo APP_MAINCLASS=%APP_MAINCLASS%
echo CLASSPATH=%CLASSPATH%
echo CURRENT_DATE=%date% %time%:~0,8%
echo ********************************************************
::执行java
echo Starting %APP_MAINCLASS% ...
echo.
"%JAVA_HOME%\bin\java" -classpath %CLASSPATH% %JAVA_OPTS% %APP_MAINCLASS% %arg1% %arg2%
:exit
pause