0

I've made an executable .jar file for a terminal game I've been working on. So far, I opened it by typing java -jar name.jar in the Terminal. This worked, but when I made a .sh file with the same command, the .jar file couldn't be accessed. It looked like this:

#! /bin/bash
java -jar game.jar

(doesn't work)

Later, I realized that if I specify where the jar file is, it does open.

#! /bin/bash
java -jar Desktop/playgame/game.jar

But the jar file and bash file are in the same folder, and if I were to move that folder elsewhere, that file path specified in the bash file won't be valid anymore.

Is there a way to specify the location of the bash file, no matter where it is?

I have used chmod +rx bashfile.sh to make the bash file executable.

I have tried it with a .command file instead of .sh file, it did the same.

Also, I'm on a MacOS Mojave 10.14.2 if that's of any importance.

Huntbook
  • 114
  • 9
  • 2
    See: https://stackoverflow.com/questions/59895/get-the-source-directory-of-a-bash-script-from-within-the-script-itself – Cyrus Feb 03 '19 at 13:28
  • Also: [BashFAQ #28: How do I determine the location of my script? I want to read some config files from the same place.](http://mywiki.wooledge.org/BashFAQ/028) – Gordon Davisson Feb 04 '19 at 00:38

1 Answers1

1

You can also use shebang

(printf '#! /usr/bin/env java -jar\n'; cat game.jar) > game
chmod +x game

and if game's dir is in PATH you can invoke just

$ game
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134