Reputation: 603
I searched the suggested answers, but i failed to find working answer for me.
Makefile compiles everything with any error, but while trying to execute my java program I get error Could not find main class bin.Program.class
Even if trying to compile program alone the same error pops out.
I have multiple .java
files, which has different classes in them and i'm using one library(.jar) file in directory lib/
. Without it java compilator gives multiple errors.
SRC = src
LIBDIR = lib
BIN = bin
JAVA_FILES=${wildcard ${SRC}/*.java}
CLASS_FILES=${JAVA_FILES:${SRC}/%.java=${BIN}/%.class}
CLASSPATH="${LIBDIR}/*"
all: run
run: ${CLASS_FILES}
java ${CLASS_FILES} -cp ${CLASSPATH}
${CLASS_FILES}: ${JAVA_FILES}
javac $^ -d ${BIN} -cp ${CLASSPATH}
Eclipse compiles and runs program fine, and Program.java has main class.
Upvotes: 1
Views: 1525
Reputation: 603
With the answer given, it really helped, and makefile now looks like and it works:
SRC = src
LIBDIR = lib
BIN = bin
JAVA_FILES=${wildcard ${SRC}/*.java}
CLASS_FILES=${JAVA_FILES:${SRC}/%.java=${BIN}/%.class}
CLASSPATH="${LIBDIR}/*"
all: run
run: ${CLASS_FILES}
java -cp ${BIN}:${LIBDIR}/* Program
${CLASS_FILES}: ${JAVA_FILES}
javac $^ -d ${BIN} -cp ${CLASSPATH}
Upvotes: 0
Reputation: 481
You should be inside of the bin directory and use
java -cp ./ Program
the bin is just a folder, so don't put that in. And Program.class is not the name of the class, Program is.
Upvotes: 2