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?

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
erotsppa
  • 14,248
  • 33
  • 123
  • 181

2 Answers2

1

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
  • 136,902
  • 23
  • 188
  • 247
  • `java -jar` does not use the `CLASSPATH` environment variable. It only uses the `Class-Path` entry from the manifest file. – user85421 Jul 07 '10 at 22:40
1

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
  • 346,391
  • 90
  • 374
  • 439