Javac junit gives "error: package org.junit does not exist"

I am trying to use JUnit in a makefile, but I cannot get it to work.

My folder structure is as follows (makefile is in myProject):

myProject |--bin |--main |--org |--myPackage |--test |--org | |--myPackage | |--lib 

where / main contains the main files, / test contains the test files and / lib contains hamcrest-core-1.3.jar and junit-4.12.jar

My makefile looks like this:

 JAVAC = javac JVM = java JAVADOC = javadoc MKBIN = mkdir -p bin JAVAC_FLAGS = -g -d bin/ JAVAC_CP = -cp SRC = main/ SRCTEST = test/ LIB = lib/*.jar PACKAGE = org/myPackage/*.java TARGET = bin MAIN = org.myPackage.Main .SUFFIXES : .class .java all: $(MKBIN) | $(JAVAC) $(JAVAC_FLAGS) $(SRC)$(PACKAGE) test: $(MKBIN) | $(JAVAC) $(JAVAC_CP) $(LIB) $(SRCTEST)$(PACKAGE) clean: rm -rf $(TARGET) run: $(JVM) $(JAVAC_CP) $(TARGET) $(MAIN) .PHONY: all test clean 

When I run make test , I get the following:

 ~/myProject | 18:07:29>make test mkdir -p bin | javac -cp lib/*.jar test/org/myPackage/*.java test/org/myPackage/MyClass.java:3: error: package org.junit does not exist import static org.junit.Assert.*; ... 

In Eclipse, tests work fine. What am I doing wrong?

+7
java junit javac makefile
source share
1 answer

CHANGE ANSWER TO

Ok, so I read a few more changes and made some changes.

First in my structure:

 |--bin |--src |--main | |--java | |--myPackage |--test |--java | |--myPackage |--lib 

from here .

And my new make file:

 JAVAC = javac JVM = java JAVADOC = javadoc MKBIN = mkdir -p bin JAVAC_FLAGS = -g -d 'bin/' JAVAC_CP = -cp MAINSRC = src/main/java/ TESTSRC = src/test/java/ LIB = 'src/test/lib/*:src/main/java' PKGSRC = myPackage/ TARGET = bin MAIN = myPackage.Main MAINTEST = myPackage.MainTest .SUFFIXES : .class .java all: $(MKBIN) $(JAVAC) $(JAVAC_FLAGS) $(MAINSRC)$(PKGSRC)* test: $(JAVAC) $(JAVAC_FLAGS) $(JAVAC_CP) $(LIB) $(TESTSRC)$(PKGSRC)* clean: rm -rf $(TARGET) run: $(JVM) $(JAVAC_CP) $(TARGET) $(MAIN) run_test: $(JVM) $(JAVAC_CP) $(TARGET) $(MAINTEST) .PHONY: all test clean run run_test 

So the changes are:

 LIB = 'src/test/lib/*:src/main/java' 
  • Quotes Around Class Path
  • * instead of * .jar
  • Path to the main
  • The classpath for class files must not include *
  • Several files are separated by the width of ':' on Linux and ';' on windows

from here .

 JAVAC_FLAGS = -g -d 'bin/' 

I forgot to include $(JAVAC_FLAGS) in test so that it doesn't aim at the right folder (root / instead of bin /).

Thanks for the help!

+4
source share

All Articles