简体   繁体   English

Linux Shell脚本,用于使用参数调用Java程序

[英]Linux shell script for calling Java program with arguments

I have a java program that runs as a service in linux box. 我有一个在Linux框中作为服务运行的Java程序。 I have shell script file that has the following line to start the program. 我有shell脚本文件,其中包含以下行来启动程序。

$EXEC -home "$JAVA_HOME" -cp "$CLASS_PATH" -outfile "$LOG_OUT" -errfile "$LOG_ERR" -pidfile "$PID" $1 $CLASS 

$CLASS_PATH has class path
$CLASS has the name of main class
EXEC="/usr/bin/jsvc"

I can start and stop the service using following commands 我可以使用以下命令启动和停止服务

  • service myscriptfilename start 服务myscriptfilename开始

  • service myscriptfilename stop 服务myscriptfilename停止

Now I added a new argument to my program called "myflag" . 现在,我在程序中添加了一个名为“ myflag”的新参数。 It works fine on windows box . 在Windows盒子上工作正常。 Now I am having difficulty passing the new argument to my program on my linux box using the shell script. 现在,我很难使用shell脚本将新参数传递给Linux机器上的程序。

Now I am starting my service as 现在,我开始以

  • service myscriptfilename start myflag 服务myscriptfilename启动myflag

I can get the value of myflag using $2 in shell script. 我可以在shell脚本中使用$ 2获得myflag的值。 I am trying to figure out how do I pass that to my program 我想弄清楚如何将其传递给我的程序

How can i pass my "myflag" to my program from shell script in the following line? 我如何在下一行中将“ myflag”从外壳程序脚本传递给我的程序?

$EXEC -home "$JAVA_HOME" -cp "$CLASS_PATH" -outfile "$LOG_OUT" -errfile "$LOG_ERR" -pidfile "$PID" $1 $CLASS

I am considering that $EXEC is java executable, $1 is your JAR, $CLASS is your main class. 我正在考虑$EXEC是Java可执行文件, $1是您的JAR, $CLASS是您的主类。 In this case just append ${@:2} to the end of the line: 在这种情况下,只需将${@:2}附加到行末:

$EXEC -home "$JAVA_HOME" -cp "$CLASS_PATH" -outfile "$LOG_OUT" -errfile "$LOG_ERR" -pidfile "$PID" $1 $CLASS ${@:2}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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