java – 无法从MAC上的脚本访问jarfile
作者:互联网
我正在尝试编写一个运行.jar文件的脚本.
以下命令适用于终端(来自.jar文件夹):
java -jar "./myjavatest.jar"
当我将这一行放在一个文件中并从同一个文件夹运行它(使用终端或双击查找器)时,我得到“无法访问jarfile ./myjavatest.jar”.
我发现当我双击一个文件时,它实际上是从用户主目录运行的.
是否可以从文件所在的文件夹中运行它?
解决方法:
I figured out that when I double click a file, it actually runs from the user home directory. Is it possible to run it from the folder that the file is located at?
就在这里:
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
标签:java,linux,executable-jar,macos 来源: https://codeday.me/bug/20190620/1246429.html