简体   繁体   中英

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:

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM