• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NoClassDefFoundError: junit.framework.Test in Maven

 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Kousik Majumder
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic