简体   繁体   English

如何运行 .bat 文件以在运行 .jar 的第二个 .bat 文件中执行命令?

[英]How do I run a .bat file for commands to execute in a second .bat file running a .jar?

Hey community I need to know how I can run .bat file to eacute a string of text to a .bat file running a .jar to use the command.嘿社区我需要知道如何运行 .bat 文件以将文本字符串发送到运行 .jar 的 .bat 文件以使用该命令。

also the batch file to run the command in the other batch file will be run from Windows Task Scheduler.另一个批处理文件中运行命令的批处理文件也将从 Windows 任务计划程序运行。

This is for a Minecraft server by the way.顺便说一句,这是用于 Minecraft 服务器的。 anyhow below is the code used to run the .jar in the batch file.无论如何,下面是用于在批处理文件中运行 .jar 的代码。 the command syntax is just "stop" .命令语法只是"stop"


server.bat code server.bat代码

java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui.

So in long terms I need to make a batch file called stop.bat to be run from Windows Task Scheduler .因此,从长远来看,我需要制作一个名为stop.bat的批处理文件,以便从 Windows Task Scheduler运行。 In the stop.bat file I need the file to exacute the command in the server.bat file so that the server will stop, after that I can just run the server.bat to start the server again.stop.bat文件中,我需要该文件来执行server.bat文件中的命令,以便服务器停止,之后我可以运行server.bat再次启动服务器。

any thoughts on how this can be done?关于如何做到这一点的任何想法? basic lay out how this should all work is below.下面列出了这一切应该如何工作的基本信息。

server-run.bat >>> "Runs" >>> minecraft-server.jar server-run.bat >>> "运行" >>> minecraft-server.jar

Task Scheduler >>> "Runs" >>> stop.bat任务计划程序 >>> “运行” >>> stop.bat

stop.bat >>> "Executes command in" >>> server-run.bat stop.bat >>> "执行命令" >>> server-run.bat

assuming your server-run.bat file looks something like this:假设您的 server-run.bat 文件看起来像这样:

@echo off
:startserver
java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui.
:running

pause
REM or whatever you do normally to stop the server
:stopserver
java stop
REM or whatever the command to stop the server is
exit

for the file to be able to jump to some mark defined by acalling batch file, you should change it to:为了使文件能够跳转到调用批处理文件定义的某个标记,您应该将其更改为:

@echo off
if "%*" NEQ "" goto %*
:startserver
java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui.
:running

pause
REM or whatever you do normally to stop the server
:stopserver
java stop
REM or whatever the command to stop the server is
exit

and stop.bat should look aproximately like this: stop.bat 应该看起来像这样:

@echo off
start server-run.bat stopserver

remem^ber this is just another way of running the server-stop-command directly from stop.bat.请记住,这只是直接从 stop.bat 运行 server-stop-command 的另一种方式。 if that wouldn't work (beacause it needs an environnement variable or something) this approach will also fail!如果这不起作用(因为它需要一个环境变量或其他东西)这种方法也会失败!

(this is beacause you can't directly interfere with a running batch file or its variables, you can only run a copy of it at the same time) (这是因为您不能直接干扰正在运行的批处理文件或其变量,您只能同时运行它的副本)

if you wnat to interfere with a running batch file, the easiest way would be to check periodically if a file exists:如果您想干扰正在运行的批处理文件,最简单的方法是定期检查文件是否存在:

@echo off
:startserver
if exists stopfile.txt del stopfile.txt
java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui.
:running

if exists stopfile.txt goto stopserver
timeout /T 2
goto running 


:stopserver
del stopfile.txt
java stop
REM or whatever the commabd to stop the server is
goto :EOF
exit

and stop.bat just does this: stop.bat 就是这样做的:

@echo off
echo.something >stopfile.txt
exit

this will cause the server-running.bat to这将导致 server-running.bat

-run the server - 运行服务器

-check every 2 seconds if stopfile.txt apperared - 如果 stopfile.txt 出现,每 2 秒检查一次

-if it did, stop the server - 如果是,停止服务器

(remember to change the dummy lines with the java code, i have no idea if they are right) (记得用java代码更改虚拟行,我不知道它们是否正确)

hope this helped in the end希望这最终有所帮助

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

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