[Pro-test effective] BAT batch script executes Jar package, using the built-in Java environment Jre

problem scenario

After writing a jar package, which uses the new features of Java8, it needs to be released to customers. Considering that customers may not necessarily use Java8, they released JRE8 together, divided into two versions, Windows and Linux, and wrote a version for each script to run.


screenplay

Windows:

client.bat

@echo off

rem The running environment of test.jar is jdk1.8 at least.

if "%OS%" == "Windows_NT" setlocal
rem ---------------------------------------------------------------------------
rem Start script for the xxx client
rem ---------------------------------------------------------------------------

set "CURRENT_DIR=%cd%"
set "JRE_HOME=%CURRENT_DIR%"
if exist "%JRE_HOME%\jre\bin\java.exe" goto okHome
cd ..
set "JRE_HOME=%cd%"
cd "%CURRENT_DIR%"
:okHome

set "EXECUTABLE=%JRE_HOME%\jre\bin\java.exe"

rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end

:okExec

rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

cd jre\bin
java -jar %JRE_HOME%\test.jar %CMD_LINE_ARGS%

:end

Linux:

client.sh

#!/usr/bin/env bash

# This is a shell for running test.

var="test"
dir=`pwd`

if [ ${var} = ${dir##*/} ];
   then
      ./jre/bin/java -jar ${dir}/test.jar "$@"
else
    echo "U should enter in directory ${var}, and type ./start.sh parameterlist"
fi

Notice


The jre/bin directory needs to be replaced with your own local jre directory folder name

test.jar needs to be modified to the jar package name of your own project

Reference https://blog.csdn.net/sand_clock/article/details/72520452

Guess you like

Origin blog.csdn.net/zzj_csdn/article/details/123574303