1

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

top:
    javac -d classfiles -sourcepath src src/kruskal.java
test:
    echo java -classpath classfiles kruskal \$\* > kruskal
    chmod +x kruskal
clean:
    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:

java.io.FileNotFoundException: classfiles (Access is denied)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at kruskal.readInputs(kruskal.java:20)
    at kruskal.main(kruskal.java:172)
Exception in thread "main" java.lang.NullPointerException
    at kruskal.BFS(kruskal.java:132)
    at kruskal.main(kruskal.java:177)

1 Answer 1

2

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:

test:
         echo "java -classpath classfiles kruskal \$$*" > kruskal
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Turns out it was indeed expanding the files in the current directory and using them as the arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.