【问题标题】:Can command line tool used in java opts?java opts中可以使用命令行工具吗?
【发布时间】:2022-02-15 17:16:49
【问题描述】:

我希望我的程序在运行启动脚本时会这样运行:

/opt/bin/java -Dgroup.profile=GROUP_270731 -Dother=other xxx.xxx.MainClass

而我的脚本就像

#!/bin/shell

JAVA_OPTS=getOpts

${JAVA_HOME}/bin/java $JAVA_OPTS $OTHER_ARGS xxx.xxx.MainClass

getOpts(){
  # get java opts from certain file java_opts
}

我可以更改的只是 java opts 文件,我必须在其中获取某个文件中的组信息,我在我的 java opts 中这样做了:

-Dgroup.profile=$(cat /home/admin/conf/deploy.conf | sed 's|[[:blank:]]||g' | grep group.profile= | cut -d= -f2)) -Dother.args

但是,我得到了这样的错误日志:

cannot find or load main class .home.admin.conf.deploy.conf

似乎在我的 java_opts cmd 中运行了一些东西。

【问题讨论】:

  • $() 内的命令的确切输出是什么?
  • 像 GROUP_004134213 这样的字符串
  • 你的#!线是废话。 #!/bin/shell .... 这应该是什么外壳?至少我不知道任何名为 shell 的 shell。实际上,对于您的问题,我强烈建议您使用可以执行数组的 shell(例如 zsh),因为通常的解决方案是将 Java 选项存储到数组中并在调用 java 时扩展数组。
  • 使用echo 打印整个命令行,或者如果你的shell支持它set -x
  • 我们不知道您的deploy.conf 的内容或这些转换应该做什么。它提出了一个问题,为什么您不以实际需要的格式保存文件,而不是在每次调用时转换不合适的文件。作为旁注,您可以首先使用sed op file | … 而不是cat file | sed op | …。通常,如果命令本身没有输入文件选项,您几乎总是可以使用 command <file 而不是 cat file | command

标签: java shell command-line-arguments jvm-arguments


【解决方案1】:

建议修复您的脚本:

#!/bin/shell
getOpts(){
  # get java opts from certain file java_opts
  echo "JAVA_OPTS values in one long string."
}
JAVA_OPTS="$(getOpts)"

# for debug use `set -x` and `set +x` view the actual executed command.
set -x
${JAVA_HOME}/bin/java "$JAVA_OPTS" $OTHER_ARGS xxx.xxx.MainClass
set +x

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多