How to pass command to running java application using a batch file

batch filejavaminecraft

He this may seem a little basic and simple but I never did this before.

I have this minecraft server and It starts up wit a batch file containing:

java -Xms1012M -Xmx1012M -jar craftbukkit-1.5.1-R0.1.jar -o true
PAUSE

Now I really simple want to create a batch that stops the server. In cmd I can just type in stop and the server stops. How can I pass this command to the running java application.

Some pseudo code:

java craftbukkit-1.5.1-R0.1.jar stop

I really have no idea. I searched the internet But I only find tutorials about making a start.bat.

A link with a tutorial will also make me happy. It just happens to be I can't find a good tutorial which covers this.

Now I found this script for linux:

 #!/bin/bash

    # Made by Devils Child.
    ##############################################################
    DIRECTORY='/home/minecraft'
    SESSIONNAME='minecraftserverscreen'
    JARFILE='craftbukkit.jar'
    WORLDNAME='world'
    NETHERNAME='world_nether'
    SKYNAME='world_skylands'
    ENDNAME='world_the_end'
    ##############################################################

start()
{
    SCREENID=$(screen -ls | grep -w $SESSIONNAME | cut -f 2)
    if [ $SCREENID ]; then
        echo "Minecraft is already running."
    else
        echo "Starting Minecraft server..."
        screen -dmS $SESSIONNAME java -Xms768M -Xmx768M -jar $JARFILE nogui
    fi
}

stop()
{
    SCREENID=$(screen -ls | grep -w $SESSIONNAME | cut -f 2)
    if [ $SCREENID ]; then
        echo "Stopping Minecraft server..."
        screen -S $SESSIONNAME -p 0 -X stuff "`printf "stop\r"`"
        while screen -ls | grep $SESSIONNAME > /dev/null
        do
            sleep 0.1
        done
    else
        echo "Minecraft is not running."
    fi
}

restart()
{
    echo "Restarting Minecraft server..."
    stop > /dev/null && start > /dev/null
}

saveoff()
{
    screen -S $SESSIONNAME -p 0 -X eval 'stuff "say Starting backup..."\015'
    screen -S $SESSIONNAME -p 0 -X eval 'stuff "save-off"\015'
    screen -S $SESSIONNAME -p 0 -X eval 'stuff "save-all"\015'
    sync
}

saveon()
{
    screen -S $SESSIONNAME -p 0 -X eval 'stuff "save-on"\015'
    screen -S $SESSIONNAME -p 0 -X eval 'stuff "say Backup complete."\015'
}

backup()
{
    echo "Creating backup of '$WORLDNAME', '$NETHERNAME', '$SKYNAME' and '$ENDNAME'..."
    saveoff
    sleep 5
    mkdir -p backups
    tar cfv "backups/NEW.tar" $WORLDNAME $NETHERNAME $SKYNAME $ENDNAME > /dev/null
    mv "backups/NEW.tar" "backups/$(date +"%m-%d-%Y_%H.%M.%S").tar"
    saveon
}

restore()
{
    echo "Restoring '$WORLDNAME', '$NETHERNAME', '$SKYNAME' and '$ENDNAME'..."
    stop > /dev/null
    rm -r $WORLDNAME
    rm -r $NETHERNAME
    rm -r $SKYNAME
    rm -r $ENDNAME
    tar xfv "backups/$(ls backups -t | head -1 | grep ".tar")" > /dev/null
    start > /dev/null
}

cd $DIRECTORY

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    backup)
        backup
    ;; 
    restore)
        restore
    ;;
    *)
        echo "Usage: ${0} {start|stop|restart|backup|restore}"
        exit 2
esac
exit 0

But my server is running an a WindowsXP machine so is there a way to do the above in windows? I have no idea.

Best Answer

I found a way to do this with AutoHotkey:

Step 1: Download->autohotkey.exe

Step 2: Install->autohotkey.exe

Step 3: Write this script and call it (stop.ahk):

SetTitleMatchMode, 2      ; mode 2 = title only needs to contain string to match
SetKeyDelay, 0            ; set key delay to minimal
#WinActivateForce         ; automatically use more forceful methods
IfWinExist, run-minecraft ; check if there is a cmd window with this title
{                         ; send the commands
    ControlSend, ,say Server will shutdown in 10 seconds, run-minecraft
    ControlSend, ,{enter}
    sleep, 9999
    ControlSend, ,stop, run-minecraft
    ControlSend, ,{Enter}, run-minecraft
    return
}

Step 4: Put title run-minecraft In front of your start.bat

Step 5: Left click (stop.ahk) And select compile script

Step 6: run application with windows task-manager