2

I've made an executable jar file for a terminal game that can be opened by typing java -jar name.jar in the Terminal.

Then I made a .sh file inside the same folder as the jar file to open it by double-clicking the .sh. I asked how to do this here, where people told me to use the following code in the .sh.

#! /bin/bash
DIR=$(dirname "$0")
java -jar "$DIR/game.jar"

This worked for a while, but when I renamed the folder, I realised if I move the folder to a pen drive the whole thing stops working and I get this in the Terminal.

Error: Unable to access jarfile /Volumes/Hard
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

So how to find the file path to the folder the .sh and the jar are in, regardless of where it is, what its name is and what drive it is on?

Also, I'm using MacOS Mojave 10.14.4 if that's of any importance.

Thank you in advance!

Huntbook
  • 23
  • 5
  • Your code will break if the path contains spaces, but that does not seem to be the problem here. What was the correct location and filename of your jar file when you executed that code? – Michael Jaros May 10 '19 at 14:51
  • do you actually mean you want to have a shell script, that would run a jar whose location is **unknown** ?? – nullPointer May 10 '19 at 14:56
  • @funkyjelly No, the shell and the jar are in the same folder. I just want that folder to be moveable without the opening system breaking. For that I need a way to find the file path to the shell script. – Huntbook May 10 '19 at 15:49
  • @MichaelJaros just to remove the distractions re: word splitting, I suggested an edit. – ctt May 10 '19 at 15:59
  • @ctt Yes I voted against that edit, but it looks like someone with 8 reputation overruled me :-) Of course those should be quoted, but I think it adds to the confusion if a question is not perfectly clear already, and then someone alters the OP's source code. – Michael Jaros May 10 '19 at 16:08
  • @Huntbook The jar path that you posted in your comment is different from the one in the error message. It looks like the jar was in `Desktop/game/` while your script was in `Desktop/`. – Michael Jaros May 10 '19 at 16:16
  • @MichaelJaros Maybe it thinks I want to open the folder as a jar file, instead of the jar inside of it, and that's why it says it's invalid or corrupt. If that's the case how do I fix it? – Huntbook May 10 '19 at 17:38
  • Did you check my presumption? It looked like your jar and script were not in the same dir at that time. – Michael Jaros May 10 '19 at 18:45

1 Answers1

0

You can store the full path of the working directory using the environement variable $PWD, like in this example (done in 5min, it is just to show you how it is works) :

#!/bin/bash
DIR=$PWD
gamePath='java -jar '$DIR'/game.jar'
echo $gamePath

Wherever I will execute this script, it will shows up the working directory even if I change the name of the parent. Let me show you :

enter image description here

You can see that $PWD environnment variable works great. Now, I will change the directory name from TestFolder to TestFolderRenamed and execute the script again :

enter image description here

So, in your case, change your code as following :

#! /bin/bash
DIR=$PWD
java -jar "$DIR/game.jar"

It should works.

Jeriko
  • 48
  • 5