简体   繁体   English

如何使用shell脚本将参数传递给java代码

[英]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-我只是对程序有一个模糊的想法,就像这样——

  1. make an executable jar file制作一个可执行的jar文件
  2. 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

  3. Now the doubts are-现在的疑问是——

    • There are so many files in my java eclipse project.我的java eclipse项目中有这么多文件。 Do I need to make an executable jar of only the file having main() method?我是否需要制作只有具有 main() 方法的文件的可执行 jar?
    • Will the arguments I supply using script be collected by the main function.我使用脚本提供的参数是否会被 main 函数收集。
    • Whats the syntax of passing arguments in shell.在 shell 中传递参数的语法是什么。 Like $var or var or "var or something else像 $var 或 var 或 "var 或其他东西

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM