Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoClassDefFoundError when running test in project with classifiers #775

Open
lkachun9129 opened this issue Aug 13, 2019 · 24 comments
Open
Labels

Comments

@lkachun9129
Copy link

VSCode version: 1.37.0
Java Test Runner version: 0.19.0

The workspace contains two projects, i.e. A and B. Project A depends on Project B.

When executing an unit test of Project A, the test failed due to a missing Java class definition from Project B.

Caused by: java.lang.NoClassDefFoundError: a/b/c/d/e/exception/InvalidTokenException
	at a.b.c.d.f.g.registrationService(g.java:27)
	at a.b.c.d.f.g$$EnhancerBySpringCGLIB$$db27a38.CGLIB$registrationService$0(<generated>)
	at a.b.c.d.f.g$$EnhancerBySpringCGLIB$$db27a38$$FastClassBySpringCGLIB$$df65555.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:355)
	at a.b.be.d.f.g$$EnhancerBySpringCGLIB$$db27a38.registrationService(<generated>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
	... 39 more
Caused by: java.lang.ClassNotFoundException: a.b.c.d.e.exception.InvalidTokenException
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 50 more

When browsing the dependency of Project A using Java Dependency Viewer, Project B is missing from Maven Dependencies.

If I remove Project B from the workspace, Project B re-appears in the dependency list under Maven Dependencies. At this moment, the test runs perfectly fine without any issue.

I am not sure whether there are some issues on resolving classpath in mutli-project workspace.

@jdneo
Copy link
Member

jdneo commented Aug 13, 2019

Hi @lkachun9129,

Would you mind to provide a sample project for this issue? With that project we can do some investigation about this issue.

@lkachun9129
Copy link
Author

I tried to reproduce the case by creating two simple maven projects but in vain.

Is there any log file I can provide for the investigation?

@jdneo
Copy link
Member

jdneo commented Aug 13, 2019

Yes there are some places to get the log. See: https://github.com/redhat-developer/vscode-java/wiki/Troubleshooting

Besides, based on your comments: When browsing the dependency of Project A using Java Dependency Viewer, Project B is missing from Maven Dependencies.

I guess this might be an issue related with the Language Server. Anyway, let's first check if we can have more information about the issue...

@jdneo jdneo added the question label Aug 13, 2019
@lkachun9129
Copy link
Author

I will get you the log tomorrow.

The reason Project B is missing from the dependency viewer might be due to classes are directly loaded from the workspace instead of JAR file from the maven repository.

@lkachun9129
Copy link
Author

Good morning.

I have checked the log of Language Server and discovered the following

[Trace - 9:06:48 AM] Sending request 'workspace/executeCommand - (35)'.
Params: {
    "command": "vscode.java.test.runtime.classpath",
    "arguments": [
        [
            "/app/home/ronaldlee/files/gitrepo-openjdk/project-a/src/test/java/a/b/c/d/e/RegistrationServiceTest.java"
        ]
    ]
}


[Trace - 9:06:48 AM] Received response 'workspace/executeCommand - (35)' in 106ms.
Result: [
    "/app/home/ronaldlee/files/gitrepo-openjdk/project-a/target/test-classes",
    "/app/home/ronaldlee/files/gitrepo-openjdk/project-a/target/classes",

    // expected to see classes from project-b but it doesn't

    // followed by a brunch of JAR files from maven repository, details skipped for simplicity
    "/app/home/ronaldlee/.m2/repository_openjdk/********.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/********.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/********.jar"
]

Maybe the Language Server failed to include classes of workspace dependencies into the class path.

@lkachun9129
Copy link
Author

I check the same log from the two sample projects, class path is constructed correctly.

[Trace - 9:26:09 AM] Sending request 'workspace/executeCommand - (57)'.
Params: {
    "command": "vscode.java.test.runtime.classpath",
    "arguments": [
        [
            "/app/home/ronaldlee/files/gitrepo-openjdk/project-a/src/test/java/projecta/ServiceATest.java"
        ]
    ]
}


[Trace - 9:26:09 AM] Received response 'workspace/executeCommand - (57)' in 32ms.
Result: [
    "/app/home/ronaldlee/files/gitrepo-openjdk/project-a/target/test-classes",
    "/app/home/ronaldlee/files/gitrepo-openjdk/project-a/target/classes",
    "/app/home/ronaldlee/.m2/repository_openjdk/junit/junit/4.12/junit-4.12.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/org/springframework/spring-test/4.3.7.RELEASE/spring-test-4.3.7.RELEASE.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/org/springframework/spring-core/4.3.7.RELEASE/spring-core-4.3.7.RELEASE.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/commons-logging/commons-logging/1.2/commons-logging-1.2.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/org/springframework/spring-context/4.3.16.RELEASE/spring-context-4.3.16.RELEASE.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/org/springframework/spring-beans/4.3.16.RELEASE/spring-beans-4.3.16.RELEASE.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/org/springframework/spring-expression/4.3.16.RELEASE/spring-expression-4.3.16.RELEASE.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/org/springframework/spring-aop/4.3.16.RELEASE/spring-aop-4.3.16.RELEASE.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/org/springframework/spring-webmvc/4.3.16.RELEASE/spring-webmvc-4.3.16.RELEASE.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/org/springframework/spring-web/4.3.16.RELEASE/spring-web-4.3.16.RELEASE.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/org/springframework/spring-orm/4.3.16.RELEASE/spring-orm-4.3.16.RELEASE.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/org/springframework/spring-jdbc/4.3.16.RELEASE/spring-jdbc-4.3.16.RELEASE.jar",
    "/app/home/ronaldlee/.m2/repository_openjdk/org/springframework/spring-tx/4.3.16.RELEASE/spring-tx-4.3.16.RELEASE.jar",
    "/app/home/ronaldlee/files/gitrepo-openjdk/project-b/target/classes"
]

I think I should pass this issue to Language Server for their follow-up.

@jdneo
Copy link
Member

jdneo commented Aug 14, 2019

I see. Basically the test runner will ask Language Server for the classpath (as you can see in the log). Something wrong must happen inside that, which also cause the Java Dependency Viewer cannot show the project B either.

@lkachun9129 it will still be useful if there is a sample project which can repro this issue. Then I can go deep to see what's happening inside the Language Server. I'll try to make a project to repro this
issue as well

@jdneo
Copy link
Member

jdneo commented Aug 14, 2019

Sorry, could you explain more about:
I check the same log from the two sample projects, class path is constructed correctly?

@lkachun9129
Copy link
Author

I tried to create two simple maven projects to replicate the issue. However, the class path issue could not be found on the two simple projects.

The first log comes from my original projects, which is currently affected by the class path issue.

The second log comes from the two simple projects, which is not affected by the class path issue.

@jdneo
Copy link
Member

jdneo commented Aug 14, 2019

I see. Are all the dependency scope of the dependencies the same from the origin project and the sample project?

@lkachun9129
Copy link
Author

Yes.

@lkachun9129
Copy link
Author

After checking once again, I can finally reproduce the issue. The issue happens when the classifier is marked as classes

<dependency>
      <groupId>sample</groupId>
      <artifactId>project-b</artifactId>
      <version>1.0.0</version>
      <classifier>classes</classifier>
</dependency>

Sample projects attached below:
sample-projects.zip

@jdneo
Copy link
Member

jdneo commented Aug 14, 2019

Why the <classifier>classes</classifier> is needed here? If you open the sample project in Eclipse and run the test in project-a, you will get the same error.

image

@lkachun9129
Copy link
Author

Project A and Project B are separate web applications. That's why they are packed as a WAR instead of JAR.

Since Project A requires some services defined in Project B, only classes of Project B are included in Project A and hence Project B is marked as classes only in the classifier.

@jdneo
Copy link
Member

jdneo commented Aug 14, 2019

I see. But from the sample project, if I run mvn install for project-b, it won't generate project-b-1.0.0-classes.jar in my local maven repo. Please correct me if I have any misunderstanding here.

@lkachun9129
Copy link
Author

Oh I missed the attachClasses config in the Maven WAR plugin.

<plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.6</version>
          <configuration>
            <attachClasses>true</attachClasses>
            <warSourceDirectory>WebContent</warSourceDirectory>
            <failOnMissingWebXml>false</failOnMissingWebXml>
          </configuration>
</plugin>

However, this missing config is not the cause of the problem.

Here is the updated sample project:
sample-projects-v2.zip

@jdneo
Copy link
Member

jdneo commented Aug 19, 2019

Yes I can repro it now. Just curious that have you run the tests in other platforms, do they work as expect?

@lkachun9129
Copy link
Author

Tried on both RedHat Linux and Windows, both failed to run.

@jdneo
Copy link
Member

jdneo commented Aug 19, 2019

Ah, I mean, other IDEs?

@lkachun9129
Copy link
Author

No

@jdneo
Copy link
Member

jdneo commented Aug 20, 2019

Ok, let's see if we can solve it from upstream first.

@jdneo jdneo added upstream and removed question labels Sep 18, 2019
@jdneo jdneo changed the title NoClassDefFoundError when running test inside a multi-project workspace NoClassDefFoundError when running test in project with classifiers Dec 6, 2019
@ivenxu
Copy link

ivenxu commented Mar 25, 2020

@jdneo any update on this? Our projects are relying on the classifier.

@kusnier
Copy link

kusnier commented Dec 2, 2020

I have same issue. A war provides some classes which are needed in another submodule. I also use the classes classifier.

Caused by: java.lang.NoClassDefFoundError: .....

Have we any progress for a fix?

@jdneo
Copy link
Member

jdneo commented Dec 2, 2020

I guess this may require the upstream issue to be fixed first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants