Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos shell脚本等待JavaJAR文件完成_Macos_Bash_Shell_Jar - Fatal编程技术网

Macos shell脚本等待JavaJAR文件完成

Macos shell脚本等待JavaJAR文件完成,macos,bash,shell,jar,Macos,Bash,Shell,Jar,我使用的是MacOSX10.8.2 我有一个简单的bash脚本执行一个jar文件(一个带有窗口的简单helloworld gui程序) 问题是,脚本执行jar,弹出一个窗口,终端窗口被文件存在消息“占用”。它永远不会完成它的执行。。。直到我手动更精确地终止java程序 我需要能够在这个bash脚本中执行java程序,使脚本在不等待java程序完成运行的情况下退出 这将发生在将jar文件复制到/Applications/folder中的安装程序包的末尾。。。实际上,安装程序从未完成,显然是在等待s

我使用的是MacOSX10.8.2

我有一个简单的bash脚本执行一个jar文件(一个带有窗口的简单helloworld gui程序)

问题是,脚本执行jar,弹出一个窗口,终端窗口被文件存在消息“占用”。它永远不会完成它的执行。。。直到我手动更精确地终止java程序

我需要能够在这个bash脚本中执行java程序,使脚本在不等待java程序完成运行的情况下退出


这将发生在将jar文件复制到/Applications/folder中的安装程序包的末尾。。。实际上,安装程序从未完成,显然是在等待shell脚本完成其执行。

只需使用
&
java
进程放在后台即可

#!/bin/sh
if ls /Applications/javahello.jar >& /dev/null ; then
  echo "File exists."
  java -jar /Applications/javahello.jar &
  exit 0 
else
  echo "File doesn't exist."
  exit 1
fi
此外,您不需要
ls
来判断文件是否存在:

#!/bin/sh
if [ -f /Applications/javahello.jar ]; then
  echo "File exists."
  java -jar /Applications/javahello.jar &
  exit 0 
else
  echo "File doesn't exist."
  exit 1
fi
另外,由于您使用了两次文件名,如果您编辑脚本以更改文件名,或者制作一个副本以运行不同的jar文件或其他文件,您可能会在两个文件名不同的地方输入错误。shell参数将修复此问题,使其不会发生:

#!/bin/sh
jar=/Applications/javahello.jar
if [ -f "$jar" ]; then
  echo "File exists."
  java -jar "$jar" &
  exit 0 
else
  echo "File doesn't exist."
  exit 1
fi

java-jar/Applications/javahello.jar&
应该在后台运行java程序,因此不要等到它完成。
#!/bin/sh
jar=/Applications/javahello.jar
if [ -f "$jar" ]; then
  echo "File exists."
  java -jar "$jar" &
  exit 0 
else
  echo "File doesn't exist."
  exit 1
fi