views:

60

answers:

2

In a bash shell script. I tried these two versions:

java -jar abc.jar&

and

CMD="java -jar abc.jar&"
$CMD

The first verison works, and the second version complains that abc.jar cannot be found. Why?

A: 

Commands do run from current directory in a shell script.

This is why the first command in your test script worked.

The second command may not work because either java isn't in your ${PATH} or abc.jar isn't in your ${CLASSPATH}. You could echo these environment variables or set +x to debug your bash script.

Johnsyweb
`java -jar` does not use the `CLASSPATH` environment variable. It only uses the `Class-Path` entry from the manifest file.
Carlos Heuberger
+1  A: 

Bash (and others) won't let you do backgrounding (&) within the value of a variable (nor will they let you do redirection that way or pipelines). You should avoid putting commands into variables. See BashFAQ/050 for some additional information.

What is the actual error message you're getting? I bet it's something like "abc.jar& not found" (note the ampersand) because the ampersand is seen as a character in the filename.

Also, the current directory for the script is the directory that it is run from - not the directory in which it resides. You should be explicit about the directory that you want to have your file in.

java -jar /path/to/abc.jar&
Dennis Williamson