简体   繁体   English

UNIX:从终端运行但无法从bash脚本运行的Java程序命令

[英]UNIX: java program command running from terminal but not working from bash script

This command works great from terminal $java -jar $picard 该命令在终端$java -jar $picard

Whenever I put that in bash script, It gives error as: line 2: -jar: command not found 每当我将其放在bash脚本中时,它都会显示以下错误: line 2: -jar: command not found

#!/bin/sh
$java -jar $picard`

Is there any fix, thanks? 有什么解决办法,谢谢?

The $ at the beginning of the line is not part of the command; 行首的$不是命令的一部分; it is part of the shell prompt. 它是shell提示的一部分。

When you put the command in a batch file, you should not include the shell prompt. 将命令放在批处理文件中时,不应包含shell提示符。 So change it to: 因此将其更改为:

#!/bin/sh
java -jar $picard

EDIT 编辑

OP mentioned that "$java" points to the actual Java binary. OP提到“ $ java”指向实际的Java二进制文件。

If you are following naming conventions for shell scripts then $java and $picard are local variables in your shell, not environment variables, so they don't get passed onto any commands that you invoke. 如果遵循外壳程序脚本的命名约定,$java$picard是外壳程序中的局部变量,而不是环境变量,因此它们不会传递给您调用的任何命令。

To turn them into environment variables, you need to export them from your shell. 要将它们转换为环境变量,您需要从外壳中导出它们。 Whereever you set values in them is the best place to put: 在其中设置值的任何地方都是放置的最佳位置:

export java
export picard

However, this turns them into environment variables, and in that case you should make the names "all capitals" -> JAVA , PICARD . 但是,这会将它们变成环境变量,在这种情况下,应将其命名为“ all capitals”-> JAVAPICARD

You have to export the java variable which you are using to point to java path as below 您必须导出用于指向Java路径的java变量,如下所示

JAVA_CLASSPATH=/bin/jre1.8.0_101/bin/java JAVA_CLASSPATH = / bin / jre1.8.0_101 / bin / java

export PICARD_CLASSPATH=/bin/picard-tools-2.5.0/picard.jar 导出PICARD_CLASSPATH = / bin / picard-tools-2.5.0 / picard.jar

$JAVA_CLASSPATH -classpath $PICARD_CLASSPATH {Nmae of the Class from where the execution begins} $ JAVA_CLASSPATH -classpath $ PICARD_CLASSPATH {从执行开始的类的名称}

Instead of using small case you can use capital letter so it will be more readable. 除了使用小写字母,您还可以使用大写字母,以便更具可读性。

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

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