Throw java.lang.NoClassDefFoundError when running test


(bruceli97) #1

I have been blocked for at least 3 days because of this error, please help me out for this situation.

src
       |
       |- main
       |
   |
       |
   |--Java
       |
   |
  |
       |
   |
  --myclass.java
       |
   |
  |--my.xslt
       |
   |
           |
   |
       |
   |-resource folder
       |
       |
       |--test
             |
             |--java
                   |
                   |--myclassTest.java

For history reason, this is my project structure. So in settings.gradle file

include 'main', 'test'

in main\myclass.java

public class myclass{
static {
    stylesheetStream = myService.class.getResourceAsStream("my.xslt");
}

So myclass will use my.xslt.

In test\myclassTest.java,

import myclass
public class myclassTest{
   @Test
   public void test111() {
      myclass
me = new myclass();
   }
}

Obviously, myclassTest reference myclass to test myclass.

under src folder, this is parent build.gradle like

apply plugin: 'base'
subprojects {
    apply plugin: 'java'
}

In main folder, build.grale like

sourceSets {
 main {
     java.srcDir 'java'
             resources.srcDir 'resources'
 }
}

in test folder, build.gradle like

sourceSets {
 test {
   java.srcDir 'java'
          resources.srcDir 'resources'
 }
}
dependencies {
  compile project(path: ':main')
  }

But when I failed in running test, and got java.lang.NoClassDefFoundError. Obviously, myclasstest failed because it can not get myclass, the reason is myclass can not find correct path for my.xslt.

How to solve this tough problem? Please advice. I appreciate your great help.


(kellyrob) #2

Might just be in the code sample, but is your main/java directory called ‘java’ or ‘Java’. Based on the tree output, it’s capitalized, but your build.gradle has it lowercase. Is that the problem?


(Adam Murdoch) #3

Why have you made them separate projects? Why not a single project with project dir set to the parent of ‘src’? Then you can just use the default locations for everything, plus the convention that test classes depend on main classes.


(bruceli97) #4

Thanks for reply. As I said, this is history reason. Actually, the project structure is more than that.

src/
    |-main (have a java folder, it will generate main.jar file)
    |-test
  (test all main project java class)
    |-ear
  (have a java folder, it will generate ear.jar file)
      |-ant
  (have a java folder, it will generate ant.jar file)
    |-integration(have a java folder, it will generate integration.jar file)
    |-other
(have a java folder, it will generate other.jar file)

All jar files are required.

main project java depends on ear, ant, integration projects.

dependencies {
  compile project(path: ':ear')
  compile project(path: ':ant')
  compile project(path: ':integration')
 }

test project depends on main project.

compile project(path: ':ear')

Just can not figure out why run test project test task, gradle can not find my.xslt file. Even I copy my.xslt file to test->java folder. Did I need to set correct classpath for test task?

Another way I can think is, take Adam Murdoch idea, like below, and try if it works, any suggestion?

src/
    |-myNewProject
    |
  |--main
     |
  |--test
  (test all main project java class)
    |
    |-ear
  (have a java folder, it will generate ear.jar file)
      |-ant
  (have a java folder, it will generate ant.jar file)
    |-integration(have a java folder, it will generate integration.jar file)
    |-other
(have a java folder, it will generate other.jar file)

(Peter Niederwieser) #5

my.xslt should go into the resource folder, not into the java folder. And why do you load it with “myService.class.getResourceAsStream()” rather than “myclass.class.getResourceAsStream()”?. There are also several typos in your code snippets, like “resource” vs. “resources” and “java” vs. “Java”.


(bruceli97) #6

Thanks for your kind reply. Yes, some of my words are not quite right. I narrow down the problem. It is from myclass.class.getResourceAsStream(“my.xslt”);

public class myclass{
static {
    stylesheetStream = myclass.class.getResourceAsStream("my.xslt");
}

I can run successfully in Eclipse IDE and Ant build.xml. However, when I use gradle, it seems Gradle can not get correct path for my.xslt. I copy this xslt file to resource folder.

public class myclass{
static {
    stylesheetStream = myclass.class.getResourceAsStream("/my.xslt");
}

It still failed. Any suggestion on how to let Gradle to get xslt path?


(bruceli97) #7

I did search. it seems other people have the same trouble.

http://gradle.1045684.n5.nabble.com/Gradle-unable-to-run-schema-validation-test-tc4390583.html


(Peter Niederwieser) #8

Not sure these are related problems.


(Peter Niederwieser) #9

Have you tried both with and without a leading slash?


(bruceli97) #10

Yes, they are related.

We got the same problem:

getResourceAsStream can not load external file correctly when Gradle run test case.


(bruceli97) #11

I even use test classpath to point out where is xslt file, but still not working. Please help.

test{
  classpath = project(':main').files('resources/xslt') + sourceSets.test.classes + sourceSets.main.classes + project(':main').sourceSets.main.classes + configurations.testRuntime
}

(Peter Niederwieser) #12

The link you gave describes a different problem. In their case getResourceAsStream() works just fine and the test fails later.


(Peter Niederwieser) #13

I’ve just tried this scenario (testing a class that loads a resource with getResourceAsStream() in its static initializer) and it worked just fine. And even if the resource isn’t found, I don’t see how this could cause a NoClassDefFoundError (stylesheetStream would simply be null). So far, this doesn’t look like a Gradle-related problem to me.


(bruceli97) #14

Yes, I got the same problem, I can in eclipse I can run test case successfully with getResourceAsStream(). However, same test class with the same getResourceAsStream() call, It can not run successfully when I run gradle clean build command


(bruceli97) #15

Thanks Peter. Can you provide your test codes? I have posted my test codes as another topic as this original one is kind of confused. Please check this link:


(Luke Daley) #16

It seems that your problem is due to putting static resources in the java directory instead of resources as per the other topic you raised. I’m going to mark this as answered since I’m confident that’s the issue. However, if this doesn’t solve it then please just comment here and we can reopen this thread.