[英]How to pass arguments to a java code using shell script
I am new to java & shell.我是 java 和 shell 的新手。 I just have a vague idea about the procedure, which is like this-
我只是对程序有一个模糊的想法,就像这样——
make a shell script file & include this command in it制作一个shell脚本文件并在其中包含此命令
java -jar /home/usr/mystuff/jarFile.jar arg1 arg2 arg3 java -jar /home/usr/mystuff/jarFile.jar arg1 arg2 arg3
Now the doubts are-现在的疑问是——
To run a jar file in this manner, java -jar /home/usr/mystuff/jarFile.jar
.要以这种方式运行 jar 文件,请执行
java -jar /home/usr/mystuff/jarFile.jar
。 You must specify a Manifest file in your jar that has your main class.您必须在具有主类的 jar 中指定一个清单文件。 The manifest file must be placed in a
META-INF
folder in the root of your jar file.清单文件必须放在 jar 文件根目录的
META-INF
文件夹中。
For example, your MANIFEST.MF file should contain a similar entry:例如,您的 MANIFEST.MF 文件应包含类似的条目:
Main-Class: com.example.MainClass
replace com.example.MainClass
with the fully qualified name of your Main Class用您的主类的完全限定名称替换
com.example.MainClass
The entire process listed above will be taken care by your IDE such as Eclipse when you create a "Runnable Jar" from your Java project > Export option > "Runnable Jar".当您从 Java 项目 > 导出选项 > “Runnable Jar”创建“Runnable Jar”时,上面列出的整个过程将由您的 IDE(例如 Eclipse)处理。
As for passing arguments from the Script to the Jar, you can pass values directly or by using a variable from the script.至于将参数从脚本传递到 Jar,您可以直接传递值或使用脚本中的变量传递值。
For example, directly:比如直接:
java -jar /home/usr/mystuff/jarFile.jar arg1 arg2 arg3
OR through shell variables: OR 通过 shell 变量:
var1="arg1"
var2="arg2"
var3="arg3"
java -jar /home/usr/mystuff/jarFile.jar $var1 $var2 $var3
Hope this helps!希望这可以帮助!
1) You will have to be create an executable jar-file, yes (using Ant, Ivy, Maven or whatever) 1) 您必须创建一个可执行的 jar 文件,是的(使用 Ant、Ivy、Maven 或其他)
2) Arguments is collected when you toss stuff to the jar file, jeah java -jar <jar-file> "Something goes in here" -DmyCoolVariable=hello
2) 当你把东西扔到 jar 文件时收集参数, jeah
java -jar <jar-file> "Something goes in here" -DmyCoolVariable=hello
3) It depends, if you have variables in the script, then pass the variables - if you're using "static" stuff, then just pass the variable directly to the command. 3)这取决于,如果脚本中有变量,则传递变量-如果您使用“静态”内容,则只需将变量直接传递给命令。 Like this:
java -jar <jar-file> "$variables" -DmyCoolVariable=$someProperty
像这样:
java -jar <jar-file> "$variables" -DmyCoolVariable=$someProperty
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.