aldorado
aldorado

Reputation: 4864

Executing java program from bash script does not work

I am trying to run a java program (weka) from a bash script. The script takes as arguments an inputfile, an outputfile and the content of file containing the command to run the java program (environment variable $CMD). The script does not work as I wish and informs me that I use an unknown option for java. I tried to echo the command that the program sends to the shell, and the output is exactly the right command. So I assume that the echo output and the command sent to the shell are not the same.

So please tell me: What did I do wrong?

What is the difference between the output I get...

echo "java $(cat $CMD) $in > $out"

...and the command the computer gets?

java "$(cat $CMD)" $in > $out

If more information is needed, please comment!

Edit:

For those familiar with weka (or familiar with java), this is what I want to get, and what is printed to me by echo:

java -cp /usr/share/java/weka.jar weka.filters.supervised.attribute.AttributeSelection -E "weka.attributeSelection.ClassifierSubsetEval -B weka.classifiers.bayes.NaiveBayes -T" -S "weka.attributeSelection.BestFirst -D 1 -N 14" -i /home/aldorado/weka_examples/iris.arff > /home/aldorado/weka_examples/irisselected131113.txt

Upvotes: 1

Views: 3302

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328780

Add set -x in before the line which causes trouble.

That will make the computer print the command again as it understood it. You will see something like

 + 'java' '-classpath weka.jar name.of.the.main.Class' 'inputFile' > 'outputFile'

Note that quotes which the shell uses to tell you "this was one word / argument for me". It's very useful to notice problems with white space and quoting.

Note that it is very hard to get something like java "$(cat $CMD)" $in > $out working. I suggest to move the java into $CMD. That will allow you to say:

bash "./$CMD" $in > $out

or, if you make the file executable:

"./$CMD" "$in" > $out

Use "$1" in the file $CMD to get a property quoted reference to "$in":

cp="weka.jar"
cp="$cp;other.jar"
cp="$cp;more.jar"
cp="$cp;foo.jar"

java -classpath "$cp" name.of.the.main.Class "$1"

Upvotes: 3

Related Questions