1

I want to start a java app from git bash in a new window with a process name.
In win cmd it is simple start "appname" java -jar /path/to.jar for example.
But in git bash I get an error The system cannot find the file javaapp

Sidenotes

  • windows 10 environment
  • I use git bash as my primary terminal because of the unix commands available there and because of the git repository status visible inside the prompt.
  • The main reason for naming the process is that I have a shell script that starts multiple java apps, and a shell script to kill them all. For the kill part I need the PID of that process
  • I followed this great tutorial on springhow.com but in that the process is not started in the new window and if I try to use start it gives the wrong pid (not the pid of the java app) so I tried the solution in this stackoverflow answer but it does not work in bash

If it helps, this is the script (mainly inspired from the link above) to start instances:

#!/bin/bash

#------ DECLARATIONS
app_path=./target/1.0-SNAPSHOT.jar
pidFile=./pid.file
instances=1

#------ BUILD IF NEEDED
if [ ! -f $app_path ];
then
    echo Building project...
    mvn clean package
else
  echo Using existing project build
fi

#------ UPDATING INSTANCES COUNT FROM PARAM IF PRESENT
if [ ! -z "$1" ]; then
    instances=$1
fi

echo Starting "$instances" "$app_path" instances

#------ CLEARING PID FILE
> $pidFile

#------ STARTING INSTANCES
for (( i = 0; i < instances; i++ )); do
    start java -jar $app_path &
    echo $! >> $pidFile
done

echo NOTES:
echo "  " Process ids for "$app_path" instances stored in "$pidFile"
echo "  " To stop instances type Ctrl+C on each or run stop_multi_apps.sh script that will stop processes with ids from "$pidFile"

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zavael
  • 2,383
  • 1
  • 32
  • 44

1 Answers1

0

You can use cmd for this.

cmd.exe /c start \"appname\" "app"

You do not need & in this case because cmd's start starts does not wait gor it.

If you want to start your program in git bash you could do something like the following:

cmd.exe /c start \"appname\" "C:\path\to\git\bash -c <command>"
dan1st
  • 12,568
  • 8
  • 34
  • 67
  • I tried in git bash to run `cmd.exe /c start "appname" java -jar /path/to.jar` but it just switches the terminal to win cmd and does not run separate window with the app – Zavael Sep 12 '19 at 11:53
  • @Zavael you have to use backslashes and `"`. I've edited my answer. – dan1st Sep 13 '19 at 05:57
  • its weird, I had to use double slash for the `/c` parameter to accept the command... this one finally worked `cmd.exe //c start \"appName\" java -jar target/app.jar` but it started a cmd window with `\appName\ ` instead of `appName` – Zavael Sep 16 '19 at 10:18
  • I've experianced the same result but I wasn't able to fix this. – dan1st Sep 16 '19 at 11:42
  • good to know, until better answer comes up, your answer is partialy correct – Zavael Sep 16 '19 at 12:31