George Coll George Coll - 1 year ago 63
Bash Question

Shell Script for Java not working

I am using a make file to create a simple shell script to execute a Java program, the makefile follows:

javac -d classfiles -sourcepath src src/
echo java -classpath classfiles kruskal \$\* > kruskal
chmod +x kruskal
rm -f classfiles/"/".class

However, when I run make test then try to execute my program (which takes in command line arguments) java tries to run the program using the arguments "-classpath" and "classfiles" as arguments, causing the program to throw a FileNotFoundException. I'm running the program with:

kruskal g1.txt

Am I doing something wrong? This is my first time working with shell scripts, any help is appreciated.

Edit: here is the exact error as well: classfiles (Access is denied)
at Method)
at Source)
at<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at kruskal.readInputs(
at kruskal.main(
Exception in thread "main" java.lang.NullPointerException
at kruskal.BFS(
at kruskal.main(

Answer Source

To escape $ in Makefile use another $. The current content of kruskal file is actually java -classpath classpath kruskal * and * is expanded with the files in current directory which is classpath among others. Or maybe even worse the * is expanded already by shell running the echo command.

So to make the Makefile line correct:

         echo "java -classpath classfiles kruskal \$$*" > kruskal
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download