Failed to access jarfile from script on MAC

I am trying to write a script that runs a .jar file. The following command works with the terminal (from the folder of the .jar file):

java -jar "./myjavatest.jar" 

When I put this line in a file and run it from the same folder (using the terminal or double-clicking in finder), I get "Unable to access jarfile./myjavatest.jar".

I realized that when you double-click on a file, it actually starts from the user's home directory. Can I run it from the folder where the file is located?

+4
source share
3 answers

I realized that when you double-click on a file, it actually starts from the user's home directory. Can I run it from the folder where the file is located?

Yes there is:

 SOURCE="${BASH_SOURCE[0]}" DIR="$( dirname "$SOURCE" )" while [ -h "$SOURCE" ] do SOURCE="$(readlink "$SOURCE")" [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" done DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" cd "$DIR" java -jar myjavatest.jar 
+5
source

Have you tried changing permissions on the file and directory using chmod 755 javatest.jar ?

0
source

I had a problem trying to execute a .sh file with this content: java -Dlog4j.configuration = file: log4j.properties -jar -Dfile.encoding = ISO-8859-1 mainRPV.jar

the problem was that for some reason the file format was replaced with DOS instead of UNIX, probably because it was edited and saved in windows. this is due to the end of the line

try opening the file with vim and run this command: set ff = unix

Hello!

0
source

All Articles