Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file help  (Read 7380 times)

0 Members and 1 Guest are viewing this topic.

CDKrash

    Topic Starter


    Greenhorn
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 10
Batch file help
« on: October 22, 2018, 05:28:54 PM »
I have a batch file than is launches a .jar. The problem is that it won't move to the next line in the script because java is running (as intended). I can't think if the function that will call the .jar (regardless of it running or not) and then move to the next line. Any assistance would be greatly appreciated.

To give background this is for a Minecraft server (.jar) startup. I want it to note the time, start the server/.jar then wait 60 seconds then input text into the cmd window. I have tried everything that I can think of but can't get it to do anything while java is running. It's been a good while since I have coded anything but I do understand it.

Thanks,
CDKrash

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Batch file help
« Reply #1 on: October 23, 2018, 01:37:42 PM »
Post your code.  Otherwise we have no clue what you are doing.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Batch file help
« Reply #2 on: October 23, 2018, 05:36:20 PM »
If you want the batch file to continue executing, you can use the start command. eg.

Code: [Select]
start "" "C:\path\to\javaw\javaw.exe" -Xmn2048m -Xmx6G -XX:GCTimeRatio=2 -XX:ParallelGCThreads=4 -XX:+UseParNewGC -XX:MaxGCPauseMillis=2000 -XX:MaxPermSize=128m -XX:+DisableExplicitGC -jar minecraft.jar
I was trying to dereference Null Pointers before it was cool.

CDKrash

    Topic Starter


    Greenhorn
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 10
Re: Batch file help
« Reply #3 on: October 24, 2018, 06:09:40 PM »
This is the latest version that I have tried. Again, it stops once java starts and will not input any text command in the CMD box. I have this set so that if the world is shut down/restarted, it will relaunch java on it's own, hence the loop at the end. There has to be a pause in the code to allow the Minecraft server to load, ruffly 70 seconds. I have tried different versions of pause (ping, sleep, and so forth), but I think what I have below may not work. I don't know as it doesn't get that far.

Minecraft server startup


:loop
goto Startup
start Minecraft
sleep 90
goto Rules

:Startup
echo (%time%) Minecraft started.

:Minecraft
java -Xms4G -Xmx4G -jar forge-1.8-11.14.4.1563-universal.jar nogui
echo (%time%) WARNING: Minecraft closed or crashed, restarting.

:Rules
echo /gamerule doTileDrops true
echo /save-off

goto loop
« Last Edit: October 24, 2018, 06:47:57 PM by CDKrash »

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Batch file help
« Reply #4 on: October 24, 2018, 06:55:54 PM »
Just follow your logic. The code start Minecraft will never execute.  This is a basic logic flowchart.  We were required to do this for all our programs when I was in high school in the 80's.

CDKrash

    Topic Starter


    Greenhorn
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 10
Re: Batch file help
« Reply #5 on: October 24, 2018, 07:02:37 PM »
Well it does work. It does everything that is there. Except it stops/pauses after Java loads. The reason is, is because Java is holding the function as if it has not completed its task. I can follow the commands in the window and it starts with "Server started at X time, executes the start of Java.jar then waits until you close the server. Once java is no longer running it Echos the"WARNING: Minecraft closed or crashed, restarting.", then the echo /gamerule doTileDrops true and echo /save-off. Then starts all back over.

Edit, below is a excerpt from the cmd... Keep in mine that I told the Minecraft server to "stop" in order to restart the Minecraft server (java.jar) Prob could get away with removing the echo before the text commands.

C:\Users\User\Documents Folders\Server name folder>echo (20:03:46.08) Minecraft started.
(20:03:46.08) Minecraft started.

C:\Users\User\Documents Folders\Server name folder>java -Xms4G -Xmx4G -jar forge-1.8-11.14.4.1563-universal.jar nogui



*****Line after line of loading the Minecraft server*****



stop


C:\Users\User\Documents Folders\Server name folder>echo (20:05:20.97) WARNING: Minecraft closed or crashed, restarting.
(20:05:20.97) WARNING: Minecraft closed or crashed, restarting.

C:\Users\User\Documents Folders\Server name folder>echo /gamerule doTileDrops true
/gamerule doTileDrops true

C:\Users\User\Documents Folders\Server name folder>echo /save-off
/save-off

C:\Users\User\Documents Folders\Server name folder>goto loop

C:\Users\User\Documents Folders\Server name folder>goto Startup

C:\Users\User\Documents Folders\Server name folder>echo (20:05:21.04) Minecraft started.
(20:05:21.04) Minecraft started.
« Last Edit: October 24, 2018, 07:17:24 PM by CDKrash »

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Batch file help
« Reply #6 on: October 24, 2018, 07:18:24 PM »
Well, mentally step through it. Instead of writing random lines into your batch script and crossing your fingers it somehow does what you expect, figure out exactly what it does, why it does it, and how to change it to do what you expect.

First it goes to the Startup label, and echos the time.

it falls through into the :Minecraft label, where it starts the Minecraft server and *waits for it to exit*. Nothing happens in the batch because java is running. Once it exits, it assumes it crashes and echoes out information that it crashed and will be restarted.

Then it prints

Code: [Select]
/gamerule doTileDrops true
/save-off
to the screen, and goes back to the loop label and does it all over. Obviously not what you want.


The Minecraft server can accept commands on standard input, so you can echo or put your startup commands in a file and redirect that to the server with <.
Code: [Select]
:loop
echo (%time%) Minecraft started.
echo /gamerule doTileDrops true > args.txt
echo /save-off >> args.txt
java -Xms4G -Xmx4G -jar forge-1.8-11.14.4.1563-universal.jar nogui < args.txt
echo (%time%) WARNING: Minecraft closed or crashed, restarting.
goto loop





I was trying to dereference Null Pointers before it was cool.

CDKrash

    Topic Starter


    Greenhorn
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 10
Re: Batch file help
« Reply #7 on: October 24, 2018, 07:30:04 PM »
I do understand what you are saying. I am rusty at coding but I do understand it as I did 2 years of programming in college. This is just the last version that I have attempted. I have tried using statements, variables and call functions (the current).

The only problem that I see with your code, is that you are inputting the text commands before the Minecraft server loads. This actually causes the server to fail on load (tried that already). It takes the server around 1 minute to finish loading all the files. This is why I had the .jar load, then pause, then try to input the text.

The problem is that when java is called, it stops the batch file from moving forward in the script due to the function still being in process. The text commands are inputting after the server is shut down/crashes, thus completing the function.


This is what it needs to do:

echo starting server

Weather running or not, don't wait on java go to wait command
 - Start java.jar

wait 90 seconds

echo input text
echo input text

echo server crashed, restarting

loop and start all over
« Last Edit: October 24, 2018, 08:23:22 PM by CDKrash »

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Batch file help
« Reply #8 on: October 24, 2018, 08:20:12 PM »
Quote
The problem is that when java is called, it stops the batch file from moving forward in the script.

That is by design. When you start any program that uses the console directly  in this manner, no further batch instructions are run until it finishes. The start command let's you execute a file and continue the batch script while it runs, but echoing text to standard output won't go to the launched program.

Quote
The only problem that I see with your code, is that you are inputting the text commands before the Minecraft server loads. This actually causes the server to fail on load (tried that already). It takes the server around 1 minute to finish loading all the files. This is why I had the .jar load, then pause, then try to input the text.
I checked it on the Spigot server I had from a few years ago I used to use for testing some of my bukkit plugins. It evaluates the text as expected, even with the delay that is apparently now present due to it being an older build. (Observe the two gamerule changes at the end). The main downside, however, is that you cannot then type into that server's console yourself, since the input was redirected from the file.
Code: [Select]
D:\javaproj\Spigot>java -jar spigot-1.11.2.jar < runcmd.txt
*** Error, this build is outdated ***
*** Please download a new build as per instructions from https://www.spigotmc.org/ ***
*** Server will start in 15 seconds ***
Loading libraries, please wait...
[19:13:52 INFO]: Starting minecraft server version 1.11.2
[19:13:52 INFO]: Loading properties
[19:13:52 INFO]: Default game type: SURVIVAL
[19:13:52 INFO]: This server is running CraftBukkit version git-Spigot-4741400-c1aa859 (MC: 1.11.2) (Implementing API version 1.11.2-R0.1-SNAPSHOT)
[19:13:52 INFO]: Debug logging is disabled
[19:13:52 INFO]: Server Ping Player Sample Count: 12
[19:13:52 INFO]: Using 4 threads for Netty based IO
[19:13:52 INFO]: Generating keypair
[19:13:52 INFO]: Starting Minecraft server on *:25565
[19:13:52 INFO]: Using default channel type
[19:13:53 INFO]: Set PluginClassLoader as parallel capable
[19:13:53 INFO]: [WorldEdit] Loading WorldEdit v6.1.5;4651611
[19:13:53 INFO]: [GriefPrevention] Loading GriefPrevention v16.5.1
[19:13:53 INFO]: [BASeCamp_Survival_Chests] Loading BASeCamp_Survival_Chests v1.7
[19:13:53 INFO]: **** Beginning UUID conversion, this may take A LONG time ****
[19:13:53 INFO]: Preparing level "world"
[19:13:53 INFO]: -------- World Settings For [world] --------
[19:13:56 INFO]: [WorldEdit] Enabling WorldEdit v6.1.5;4651611
[19:13:56 INFO]: WEPIF: Using the Bukkit Permissions API.
[19:13:56 INFO]: [WorldEdit] Using com.sk89q.worldedit.bukkit.adapter.impl.Spigot_v1_11_R1 as the Bukkit adapter
[19:13:56 INFO]: [GriefPrevention] Enabling GriefPrevention v16.5.1
[19:13:56 INFO]: [GriefPrevention] Finished loading configuration.
[19:13:56 INFO]: [GriefPrevention] 0 total claims loaded.
[19:13:57 INFO]: [GriefPrevention] Customizable messages loaded.
[19:13:57 INFO]: [GriefPrevention] Finished loading data (File Mode).
[19:13:57 INFO]: [GriefPrevention] Boot finished.
[19:13:57 INFO]: [BASeCamp_Survival_Chests] Enabling BASeCamp_Survival_Chests v1.7
[19:13:57 INFO]: BCSURV running on Server version:1.11.2
[19:13:57 INFO]: BCSURV:survivalchests.cfg Not found. Reading from JAR...
[19:13:57 INFO]: Read in 185 elements
[19:13:57 INFO]: Read in 185 elements via Yaml.
[19:13:57 INFO]: .\plugins\BCRandomizer\Schematics
[19:13:57 INFO]: loaded schematic:desert_tomb
[19:13:57 INFO]: loaded schematic:forest_sniper
[19:13:57 INFO]: loaded schematic:jungletower
[19:13:57 INFO]: loaded schematic:loghut
[19:13:57 INFO]: loaded schematic:pillbox
[19:13:57 INFO]: loaded schematic:skeletonshack
[19:13:57 INFO]: loaded schematic:small_bunker
[19:13:57 INFO]: schematiccount=7
[19:13:57 INFO]: Server permissions file permissions.yml is empty, ignoring it
[19:13:57 INFO]: Done (3.905s)! For help, type "help" or "?"
[19:13:57 INFO]: Game rule doFireTick has been updated to false
[19:13:57 INFO]: Game rule doTileDrops has been updated to true
I was trying to dereference Null Pointers before it was cool.

CDKrash

    Topic Starter


    Greenhorn
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 10
Re: Batch file help
« Reply #9 on: October 24, 2018, 08:39:25 PM »
Exactly! Agreed. This is why I was using functions. If anything needs to be updated or edited, I can do it from the script. I currently have it set so that when play is done, users can restart the server in game and the world will go back to how it was when they first logged in. Hence why I am wanting to turn world save off on server load. This is not supported from command block and I don't want it to be on the player to remember to "disable saving"

I am not running a bukkit sever or this would be a whole lot easier. I am running a modded server. By running my server in this fashion I am able to load more mods with less issues. I tried using buckkit and spigott, but they would not allow the use of mods how I am running it and were far less stable. I'm not saying that it can't be done, but I have found that with a modded server, this is how it runs better for what I am doing.

Quote
The start command let's you execute a file and continue the batch script while it runs

This is what I was looking at but think that I am failing to execute.

Code: [Select]
19:13:57 INFO]: Server permissions file permissions.yml is empty, ignoring it
[19:13:57 INFO]: Done (3.905s)! For help, type "help" or "?"
[19:13:57 INFO]: Game rule doFireTick has been updated to false
[19:13:57 INFO]: Game rule doTileDrops has been updated to true

How did you get it to implement the rules after loading? That is exactly what I am trying to accomplish.
« Last Edit: October 24, 2018, 08:51:46 PM by CDKrash »

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Batch file help
« Reply #10 on: October 24, 2018, 09:13:09 PM »
Spigot/bukkit/etc. shouldn't make a difference in this particular case. I just happened to have a Spigot setup to test with.

Quote
This is what I was looking at but think that I am failing to execute.
Before you get too excited about it, it won't work for what you want. You would use the start command on the line that starts java itself. I gave an example already, but in your case it would be:

Code: [Select]
start "" java -Xms4G -Xmx4G -jar forge-1.8-11.14.4.1563-universal.jar nogui

This the batch file would continue while the Minecraft server started and ran. This would turn your one problem into two, as in addition to figuring out how to send commands to the running minecraft server, you would need to determine how to check if the server is still running. (Where before, that it was not was implied by the next line of batch executing).

Quote
How did you get it to implement the rules after loading? That is exactly what I am trying to accomplish.

It's in the code block, it was started with the command

Code: [Select]
java -jar spigot-1.11.2.jar < runcmd.txt
which sent the contents of runcmd.txt to the new minecraft instance's standard input. Normally standard input is the keyboard. Once the server starts it starts reading lines from standard input which it will be reading from that file. In this case runcmd.txt contained:

Code: [Select]
gamerule doFireTick false
gamerule doTileDrops true
This is what my previous batch script did, it just created the text file beforehand from the batch script.

As I said though, this means you can't use the keyboard to type commands directly into the server console, which would be annoying.

You might look into using RCON utilities to use the Remote Console feature that the server implements.
I was trying to dereference Null Pointers before it was cool.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Batch file help
« Reply #11 on: October 27, 2018, 07:59:59 AM »
In regards to my previous response, these three lines of code NEVER EXECUTE
start Minecraft
sleep 90
goto Rules

I believe you think your code is acting like a function; running the code that you went to with the LABEL and returning, but that is not what GOTO does.  Heck if memory serves me that is not what it did in BASIC, FORTRAN or Pascal back in my days of programming those languages in the late 80's and early 90's.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Batch file help
« Reply #12 on: October 27, 2018, 12:42:50 PM »
Yeah, I glossed over that somewhat myself, only mentioning it via "First it goes to the Startup label, and echos the time.". I had actually written about it but found I was erm, too angry... in the post for it to be sensible. There was too much weird stuff in their "batch file" to properly cover and the more I tried the more annoyed I became. do they think GOTO executes a label as a subroutine until it hits another label? Why did they interpret my advice regarding start as applying to that personal vision as an alternative to goto? Are they allergic to documentation? It really did look like somebody had no idea how to write batch and had sort of guessed how it would work, instead of consulting actual documentation.
I was trying to dereference Null Pointers before it was cool.

CDKrash

    Topic Starter


    Greenhorn
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 10
Re: Batch file help
« Reply #13 on: November 03, 2018, 05:02:26 PM »
Thank you for your criticism and narcissistic last responses. I was going to say that I figured it out and thank you for the assistance, but your comments at the end of this were unnecessary. As I stated in the beginning of this post, it has been YEARS since I had done this and needed assistance in completing, not ridicule. Good Day gents

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Batch file help
« Reply #14 on: November 03, 2018, 07:32:02 PM »
Would like to point out that your post from October 24th proved that all your code was not executing as you posted the verbose out of the script running.  The verbose output proves that your logic was incorrect and the lines of code I pointed out were never executing.
Well it does work. It does everything that is there.

Edit, below is a excerpt from the cmd... Keep in mine that I told the Minecraft server to "stop" in order to restart the Minecraft server (java.jar) Prob could get away with removing the echo before the text commands.

C:\Users\User\Documents Folders\Server name folder>echo (20:03:46.08) Minecraft started.
(20:03:46.08) Minecraft started.

C:\Users\User\Documents Folders\Server name folder>java -Xms4G -Xmx4G -jar forge-1.8-11.14.4.1563-universal.jar nogui



*****Line after line of loading the Minecraft server*****



stop


C:\Users\User\Documents Folders\Server name folder>echo (20:05:20.97) WARNING: Minecraft closed or crashed, restarting.
(20:05:20.97) WARNING: Minecraft closed or crashed, restarting.

C:\Users\User\Documents Folders\Server name folder>echo /gamerule doTileDrops true
/gamerule doTileDrops true

C:\Users\User\Documents Folders\Server name folder>echo /save-off
/save-off

C:\Users\User\Documents Folders\Server name folder>goto loop

C:\Users\User\Documents Folders\Server name folder>goto Startup

C:\Users\User\Documents Folders\Server name folder>echo (20:05:21.04) Minecraft started.
(20:05:21.04) Minecraft started.

patio

  • Moderator


  • Genius
  • Maud' Dib
  • Thanked: 1769
    • Yes
  • Experience: Beginner
  • OS: Windows 7
Re: Batch file help
« Reply #15 on: November 03, 2018, 08:08:31 PM »
Your refund check is in the mail...
" Anyone who goes to a psychiatrist should have his head examined. "