bash 创建一个 shell 脚本来运行 Java 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3598664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a shell script to run Java program
提问by Sameek Mishra
I used a shell script to run a Java class. My script contains
我使用了一个 shell 脚本来运行一个 Java 类。我的脚本包含
#!/bin/sh
java -jar jobs/job.jar
These are my failed attempts to run it.
这些是我运行它失败的尝试。
[root@]#sh testapp.sh
Unable to access jarfile jobs/job.jar
if I just do this at the command line it works fine
如果我只是在命令行中执行此操作,则它可以正常工作
[root@]#java -jar jobs/job.jar
thanks.
谢谢。
采纳答案by joschi
Use the absolute path to your JAR file, e. g. /root/jobs/job.jar.
使用 JAR 文件的绝对路径,例如/root/jobs/job.jar.
回答by Colin Hebert
The best way is to get the current dirname and get in there with this:
最好的方法是获取当前目录名并使用以下命令进入:
#!/bin/sh
cd `dirname "##代码##"`
java -jar ./job/job.jar

