Minimal Makefile to Run Java Projects

less than 1 minute read

Recently, a conversation with my SO reminded me about a piece of code I write long time ago. It was a college assignment on Data Structure course.

I immediately dug my email and found the project compressed in a RAR archive. The project was written using Java with NetBeans IDE default folder structure. There was no build configurtions like Maven, Ant, Makefile, or whatsoever. Only NetBeans project config and I don’t want to install it just for the sake of running the code.

After Googled a bit, I came with a quick and simple Makefile to run the code. Luckily there was no external dependencies to deal with.

SRC ?= src
DST ?= build/classes
MAIN ?= Main

.PHONY: clean compile run

	rm -f $$(find $(DST) -name *.class)

	mkdir -p $(DST)
	javac -d $(DST) $$(find $(SRC) -name *.java)

	java -cp $(DST) $(MAIN)