Problem with my bash script

That's all, I have a problem with my bash script. What is my code:

#!/bin/bash
java -jar my_app.jar
echo "The present working directory is `pwd`"

If I do it with. / script _name, it works, but if I double-click on it, it doesn’t work, I got this error:
"Could not access jarfile my_app.jar".
Then the output pwd is different.

My OS is MacOSX, but I need to create a bash script that also works on Linux.

+3
source share
4 answers

script, . , . , java , script. , .

0

, , Shawn J. Goff Gordon Davisson , bash. , script jar :

#!/bin/bash
java -jar "$(cd "$(dirname "$0")"; pwd)/my_app.jar"
echo The present working directory is $(cd "$(dirname "$0")"; pwd)
+1

$0 script. dirname, , script.

java -jar `dirname $0`/my_app.jar
0

, :

  • , script , 1 , . . script, jar .

  • java . bare java PATH. , , if [ -e /usr/bin/java ], , , .

0

Source: https://habr.com/ru/post/1763958/


All Articles