简体   繁体   English

如何通过 .bat 文件将参数传递给另一个 Java 程序?

[英]How to pass argument via a .bat file to another program for Java?

I have a question regarding the evaluation of my code using a evaluator program by accessing it via a batch file.我有一个关于使用评估程序通过批处理文件访问它来评估我的代码的问题。 I have pasted the batch file code below.我已经粘贴了下面的批处理文件代码。

@if "%DEBUG%" == "" @echo off
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%..

@rem Add default JVM options here. You can also use JAVA_OPTS and TASK_SMALLEST_POLYGON_EVALUATOR_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
echo %JAVA_EXE%
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
echo %JAVA_HOME%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=D:\Programs\Java\programming\Polygon\bin\testerexec.jar
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=D:\Programs\Java\programming\Polygon\bin\testerexec.jar

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\lib\task-smallest-polygon-evaluator.jar;%APP_HOME%\lib\scala-logging_2.13-3.9.2.jar;%APP_HOME%\lib\jackson-module-scala_2.13-2.10.2.jar;%APP_HOME%\lib\scallop_2.13-3.3.2.jar;%APP_HOME%\lib\scala-reflect-2.13.0.jar;%APP_HOME%\lib\scala-library-2.13.1.jar;%APP_HOME%\lib\logback-classic-1.2.3.jar;%APP_HOME%\lib\jackson-module-paranamer-2.10.2.jar;%APP_HOME%\lib\jackson-databind-2.10.2.jar;%APP_HOME%\lib\logback-core-1.2.3.jar;%APP_HOME%\lib\slf4j-api-1.7.26.jar;%APP_HOME%\lib\jackson-annotations-2.10.2.jar;%APP_HOME%\lib\jackson-core-2.10.2.jar;%APP_HOME%\lib\paranamer-2.8.jar
echo %JAVA_OPTS%
echo %TASK_SMALLEST_POLYGON_EVALUATOR_OPTS%
echo done
@rem Execute task-smallest-polygon-evaluator
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %TASK_SMALLEST_POLYGON_EVALUATOR_OPTS%  -classpath "%CLASSPATH%" com.simscale.smallestpolygon.Evaluator %CMD_LINE_ARGS%
pause
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

:fail
rem Set variable TASK_SMALLEST_POLYGON_EVALUATOR_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if  not "" == "%TASK_SMALLEST_POLYGON_EVALUATOR_EXIT_CONSOLE%" exit 1
exit /b 1

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega

There is an evaluator which needs to evaluate my code.有一个评估器需要评估我的代码。 I would like to know how to pass an argument to this batch file.我想知道如何将参数传递给这个批处理文件。 I tried to input in the argument a location of my .jar file but I get an error of我试图在参数中输入我的 .jar 文件的位置,但出现错误

java.io.IOException: Cannot run program "D:\\Programs\\Java\\programming\\Polygon\\bin\\testerexec.jar": CreateProcess error=193, %1 is not a valid Win32 application java.io.IOException:无法运行程序“D:\\Programs\\Java\\programming\\Polygon\\bin\\testerexec.jar”:CreateProcess 错误=193,%1 不是有效的 Win32 应用程序

ERROR com.simscale.util.Util$ - unexpected exception in thread java.io.IOException: Cannot run program "java -jar "D:\\New folder\\task-smallest-polygon-evaluator\\bin\\testerexec3.jar"": CreateProcess error=2, The system cannot find the file specified错误 com.simscale.util.Util$ - 线程 java.io.IOException 中的意外异常:无法运行程序 "java -jar "D:\\New folder\\task-smallest-polygon-evaluator\\bin\\testerexec3.jar"": CreateProcess error=2, 系统找不到指定的文件

I have tried 2 kinds of arguments in the input, but none of them seem to work我在输入中尝试了 2 种参数,但它们似乎都不起作用

An evaluator program is supplied.提供了一个评估程序。 This program receives one positional argument, which is the executable file (entry point for the target program – your program).这个程序接收一个位置参数,它是可执行文件(目标程序的入口点——你的程序)。

The positional argument I am passing is generating an error.我传递的位置参数产生错误。 Could somebody help me on how to pass this positional argument?有人可以帮助我如何传递这个位置参数吗? What inputs must be given in the bat file? bat 文件中必须给出哪些输入?

Try installing it as a window service first and run the service.首先尝试将其安装为窗口服务并运行该服务。

download the nssm using this link: https://nssm.cc/release/nssm-2.24.zip Then, install your .jar file as a windows service first.使用此链接下载 nssm: https ://nssm.cc/release/nssm-2.24.zip 然后,首先将您的 .jar 文件安装为 Windows 服务。 Sample code to install as a service: nssm install serviceName url-to-java-exec url-to-Jar-file作为服务安装的示例代码:nssm install serviceName url-to-java-exec url-to-Jar-file

Hope this is helpful希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM