*
The moose likes Ant, Maven and Other Build Tools and the fly likes NoClassDefFoundError: junit.framework.Test  in Maven Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "NoClassDefFoundError: junit.framework.Test  in Maven" Watch "NoClassDefFoundError: junit.framework.Test  in Maven" New topic
Author

NoClassDefFoundError: junit.framework.Test in Maven

Kousik Majumder
Ranch Hand

Joined: Sep 30, 2007
Posts: 229
Hi all,

I am new to Maven and trying to run a test project. I can not understand why the follwoing error is being shown. For added information I have seen that in the local repository the class "Test" is present in the package shown below.



pom.xml:-



settings.xml:-



Please suggest.


Thanks in Advance,
Kousik
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
When you are running a test, you have to specify that your dependency is on JUnit.

Try to specify the dependency for JUNIT in your pom.xml and try. I think it should resolve the error.

Hope this helps,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Kousik Majumder
Ranch Hand

Joined: Sep 30, 2007
Posts: 229
Thanks a lot Prithvi. my problem is solved.


But the problem is that jar is containing no source. Following message is showing.



How can I include class files and run it to show output?
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi,

What maven version you are using?
Another thing, where are you putting your source code exactly?

I have tried to generate the same error. I believe you are not putting your source-code in the correct directory.
By default Maven expects all your souce-code to be placed in src/main/java. It is default maven configuration.

As maven uses convention over configuration. If you don't follow the conventions, and place the source code,
somewhere else, the packaging will be empty.

How i reproduced this error was, i created a simple test project and from there, i deleted the source code from
src/main/java and placed it on src/main. Then i cleaned the project and packaged, and it created a jar for me,
but no class files included. So please sure, you are using correct convention and placing your source code in the
correct directory.


Hope this helps,
Kousik Majumder
Ranch Hand

Joined: Sep 30, 2007
Posts: 229
Hi Prithvi,

Thanks for your reply.
I am using Maven 2.0.9. Yes I am able to put the .java file in the jar.The path was wrong.

But I think I need to put the .class files in the .jar and want to run the that jar file using maven so that I can see the "Hello World" output in the console.

How can I achive this?

Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,

why you want to put .java files in a jar. A jar archive never has a .java file inside, it has only .class files
instead. If you are putting everything correctly in the correct way and packaging your build, your jar file
will be automatically have the .class files. You don't have to do anything special to put the .class files in the
jar files.

You are still doing something wrong, investigate the issue thoroughly.

Hope this helps,
Kousik Majumder
Ranch Hand

Joined: Sep 30, 2007
Posts: 229
No. I did not want to put .java file in jar.
My POM.xml is given below. I dont find any way to put .class in my .jar. Please suggest.



Also the directory structure I am getting is quite interesting. Shown below:

META-INF/maven/Test/Test/pom.xml
META-INF/maven/Test/Test/pom.properties
META-INF/MANIFEST.MF

I am


Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
What does your console debug information say.

Run

mvn package -X and see the debugging details. I am still thinking you are not placing the project source
code in the correct directory. Where are you placing the source code of your project?

Else, paste your console output here.
Kousik Majumder
Ranch Hand

Joined: Sep 30, 2007
Posts: 229
Hi,

my source code structure is given below:

Test\src\main\Test.java
Test\src\test\java\Test
Test\src\test\java\TestTest

And while packaging it is showing the following info:


[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Test
[INFO] task-segment: [package]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] No sources to compile
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] Compiling 2 source files to C:\Documents and Settings\workspace\Test\target\test-classes
[INFO] [surefire:test]
Downloading: http://integration/archiva/repository/internal/org/apache/maven/surefire/surefire-junit/2.4.2/surefire-j
unit-2.4.2.pom
[INFO] Surefire report directory: C:\Documents and Settings\workspace\Test\target\surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running Test
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.047 sec
Running TestTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.015 sec

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] [jar:jar]
[INFO] Building jar: C:\Documents and Settings\workspace\Test\target\Test-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16 seconds
[INFO] Finished at: Thu Feb 18 13:29:17 IST 2010
[INFO] Final Memory: 7M/254M
[INFO] ------------------------------------------------------------------------


Please suggest and thanks for your replies too.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Your source code main .java file is not placed in the correct location.
Additionally, it's always a good practice to avoid default packaging.

Put your Test.java file in this folder

src/main/java/Test.java
src/test/java/Test.java

correct the structure, clean the build and package it again. Do include the packaging element in your POM
also.

Hope this helps,
Kousik Majumder
Ranch Hand

Joined: Sep 30, 2007
Posts: 229
Hi Prithvi,

Thanks a lot for your help. My problem is solved after creating a new maven project. Now my jar is containing a class file which is under src\main\java.
But I am still confused.

1. Why there is src/test/java folder.
2. How to compile this folder's java file.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5812
    
    7

1) Because that is where your unit tests go.
2) The tests get compiled during Maven's test-compile phase

What Maven books have you read? There are two free PDFs online...


JBoss In Action
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: NoClassDefFoundError: junit.framework.Test in Maven
 
Similar Threads
maven release build error
Could not resolve dependencies
Richfaces + Spring + Maven Setup help
Add dependency in maven project for j2ee.jar
Error slf4j-api, I can't exclude slf4j-api for Jboss