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

I'm trying to use JUnit in a makefile but I can't 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 main files, /test contains test files and /lib contains hamcrest-core-1.3.jar and junit-4.12.jar

My makefile is as follows:

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'm running 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 the tests work perfectly fine. What am I doing wrong?



EDIT FOUND THE ANSWER

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

First in my structure:

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

from here.

And my new makefile:

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 the classpath
  • * instead of *.jar
  • Classpath to main
  • Classpath to class files should not include the *
  • Multiple files are separated width ':' in Linux and ';' in Windows
  • from here.

    JAVAC_FLAGS = -g -d 'bin/'
    

    I forgot to include $(JAVAC_FLAGS) to test so that it didn't target the right folder (root/ instead of bin/).

    Thanks for the help!

    链接地址: http://www.djcxy.com/p/82948.html

    上一篇: java.lang.OutOfMemoryError:超出GC开销限制

    下一篇: javac junit会给出“错误:package org.junit不存在”