NoClassDefFoundError even though jar is in the module path Eclipse - java

I am working on a vanilla Eclipse project and trying to avoid using Maven.
I have added Jackson modules to the module path as an external jar. When I do so, the import statement no longer shows an error but when I run my program I get the following error
Exception in thread "main" java.lang.NoClassDefFoundError:
com/fasterxml/jackson/databind/ObjectMapper at
bitcoin.PriceConverter.getJsonFromUrl(PriceConverter.java:48) at
bitcoin.PriceConverter.main(PriceConverter.java:26) Caused by:
java.lang.ClassNotFoundException:
com.fasterxml.jackson.databind.ObjectMapper at
java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at
java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 2 more
Line 48 in PriceConverter is ObjectMapper objMapper = new ObjectMapper();
I have googled extensively and found that my classpath is potentially broken however, all of the answers are from outdated versions of Eclipse and I cannot work out how to fix this problem.
I tried the same thing with Gson and had the same problem however, yesterday I added the json-20210307.jar and it worked fine.
Can anyone explain what is happening here and guide me on how to fix it?

Related

JUnit error "java.lang.NoClassDefFoundError:"

I encounter a weird issue with JUnit testing. I added a new rule to my BNF file which results in creation of a new token FOO. Running the plugin works fine and the token and element is shown in PSI View. However, when executing a UnitTest it fails with:
de/foo/bar/psi/impl/FooBarImpl
java.lang.NoClassDefFoundError: de/foo/bar/psi/impl/FooBarImpl
at de.foo.bar.psi.FooTypes$Factory.createElement(FooTypes.java:424)
at de.foo.bar.parser.FooParserDefinition.createElement(FooParserDefinition.java:63)
at com.intellij.psi.impl.source.tree.CompositeElement.createPsiNoLock(CompositeElement.java:705)
...
Caused by: java.lang.ClassNotFoundException: de.foo.bar.psi.impl.FooTinesImpl
at com.intellij.util.lang.UrlClassLoader.findClass(UrlClassLoader.java:215)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 40 more
The class reported as not found is available.
Got it working by deleting the folder build/classes from root of my plugin folder.

java.lang.NoClassDefFoundError when running via Eclipse

I get the following error messages when attempting to run my program via Eclipse:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/JavaVersion
at lemmini.LemminiFrame.main(LemminiFrame.java:762)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.JavaVersion
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
The project has all necessary dependencies loaded and there are now 0 errors in the code (there are some warnings though).
Ideas welcome. Thank you!
OK, it turns out that the issues I've been experiencing are all to do with the dependencies. I loaded them into a folder called 'dependencies' within the project folders (alongside 'src') and then imported them from there via Project>Properties>Java Build Path>Libraries (then choose Classpath>Add JARs)
I can now run the program within Eclipse, but can't run it as an executable .jar
The journey continues...

Jackson dependency issue on Eclipse: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonFactory

I am new to jackson. I have set up a test sample to make sure that I am able to use jackson on my Eclipse environment. However, it seems that I got some unexpected error while compiling my simple code on Eclipse, and I have searched a bit and do not know how to resolve it. Any advice is greatly appreciated.
I have added the .jar files to the "Build Path" of my project, as shown below.
My code is quite simple just for testing the dependency:
import com.fasterxml.jackson.core.JsonFactory;
public class JacksonTest {
public static void main(String[] args) {
JsonFactory jsonFactory = new JsonFactory();
}
}
When I compile the code on Eclipse, it throws an error: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory
More details of the error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory
at jacksontest.JacksonTest.main(JacksonTest.java:25)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
Could anyone tell how to resolve it? Thanks a lot in advance.
I found the answer myself. Silly me! I should add the jar to classpath not modulepath

JUnit doesnt run

Ive been playing with junit over the past few weeks and Ive switched to a diffrent machine and for the life of me I cannot get JUnit to run successfully. This is the error that eclipse is giving me:
java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:333)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadTestLoaderClass(RemoteTestRunner.java:380)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:370)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:365)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:309)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:224)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:208)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.manipulation.Filter
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:604)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 8 more
I have my environment variables set correctly; JUNIT_HOME [points to c:/junit-4.10.jar] and CLASSPATH [points to %CLASSPATH%;%JUNIT_HOME%\JUnit4.10.jar;.;]
I hope someone can help me out with this, thanks!!
I managed to fix the issue, I had to go to java-->build path and I removed the .jar and the junit library from modulepath, I then added only the junit library under classpath and it works fine now; I am still confused, when I add the junit library it says source not found.
It looks like you are using the wrong version of JUnit and it doesn't have the classes or methods you are expecting.

Where do I get the missing resources?

I am still unable to get the box-api hello world program to work. I was getting this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/NotImplementedException
at TestProgram.getAuthenticatedClient(TestProgram.java:47)
at TestProgram.main(TestProgram.java:35)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.NotImplementedException
...
I then went to the Apache Commons Lang Home and downloaded and set up their thing.
Now I get
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
at com.box.boxjavalibv2.jacksonparser.BoxResourceHub.<init>(BoxResourceHub.java:28)
at com.box.boxjavalibv2.BoxClient.createResourceHub(BoxClient.java:345)
at com.box.boxjavalibv2.BoxClient.<init>(BoxClient.java:65)
...
and I have no idea how to fulfill that dependency. I also suspect that even if I could find this one, there'd just be another one, and another one, and another one. Manually importing them all does not seem to be very realistic.
How am I intended to get the missing dependencies for the box API?
The SDK comes included with all of the dependencies you should need. You can find them all in the libs directory of the SDK..
Add all of those jars to your Eclipse build path and it should work.

Categories

Resources