bash 通过bash脚本执行jar文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36379203/
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
executing a jar file through a bash script
提问by udarakr
I want to execute a jar file using java -jar command from a bash script. At the same time I'm passing few parameters which I can access in my main method, using args[].
我想使用 bash 脚本中的 java -jar 命令执行 jar 文件。同时,我传递了几个可以在主方法中使用 args[] 访问的参数。
I'm able to do this without any issues if I try,
如果我尝试,我可以毫无问题地做到这一点,
java -jar org.sample.jar localhost 8080
but I want to make these parameters configurable through few variables.
但我想通过几个变量来配置这些参数。
Eg:-
例如:-
HOST=localhost
PORT=8080
When I try to use above variables like following, bash script fails silently.
当我尝试使用上述变量时,bash 脚本无声无息地失败。
java -jar org.sample.jar $HOST $PORT
I also tried using "$HOST" but without any luck. What am I doing wrong here?
我也尝试使用“$HOST”但没有任何运气。我在这里做错了什么?
UPDATE
更新
I was using cygwin to execute/debug my bash script. Once I execute the same script using git bash it works.
我正在使用 cygwin 来执行/调试我的 bash 脚本。一旦我使用 git bash 执行相同的脚本,它就会工作。
回答by Keqiang Li
You should wrap this in a bash scripts and using bash parameters.
您应该将其包装在 bash 脚本中并使用 bash 参数。
Say you will have a script file sample.sh.
假设您将有一个脚本文件 sample.sh。
in sample.sh, it contains only one line, which is java -jar org.sample.jar "$@"
在sample.sh中,它只包含一行,即 java -jar org.sample.jar "$@"
Then execute your jar file by bash sample.sh -Host hostname -port 8080
然后执行你的jar文件 bash sample.sh -Host hostname -port 8080
Here is a post about how to do this.
UPDATE
更新
To use variables in your bash script, your script will look like this:
要在 bash 脚本中使用变量,您的脚本将如下所示:
Host="localhost"
Port=8080
java -jar org.sample.jar $Host $Port
I'm using Eclipse and I only wrote one class which has a main method and prints all the arguments. Then I used Eclipse to export a jar file and then used this solution and it worked.
我正在使用 Eclipse,我只写了一个类,它有一个 main 方法并打印所有参数。然后我使用 Eclipse 导出了一个 jar 文件,然后使用了这个解决方案并且它起作用了。