JUnit doesnt run - java

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.

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.

NoClassDefFoundError even though jar is in the module path Eclipse

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?

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...

Error while trying out JUnit

I have done more than an hour of searching while trying to run JUnit on my project. I can see that there is a class missing - LogEntryFormatter. But no matter how hard I tried, I am not able to find the jar file which contains this one. Eclipse shows the below stack trace after running the Test case file.
java.lang.NoClassDefFoundError: weblogic/logging/LogEntryFormatter
at java.lang.ClassLoader.findBootstrapClass(Native Method)
at java.lang.ClassLoader.findBootstrapClass0(ClassLoader.java:892)
at java.lang.ClassLoader.loadClass(ClassLoader.java:302)
at java.lang.ClassLoader.loadClass(ClassLoader.java:300)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at weblogic.logging.commons.LogImpl.(LogImpl.java:14)
at weblogic.logging.commons.LogFactoryImpl.getInstance(LogFactoryImpl.java:21)
at weblogic.logging.commons.LogFactoryImpl.getInstance(LogFactoryImpl.java:18)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.(SpringJUnit4ClassRunner.java:91)
...
When I use #RunWith(SpringJUnit4ClassRunner.class) and if I use #RunWith(JUnit4.class), a sample test seems to work.
Any sort of help will be useful. I am using WebLogic server, and all weblogic related jars are available on the classpath.
Ok, so after a lot of effort I am able to run JUnit Tests, although not in the exact way I want. I am also at a loss to explain why this is happening or where weblogic.logging is configured. This could be a possible problem with my project setup. After adding the following jar files to the classpath (after removing everything except jdk), it seems to be working for me.
wlclient
com.bea.core.utils.classloaders
com.bea.core.descriptor
com.bea.core.utils
com.bea.core.management.core
junit 4.5
Thanks to all those who helped by providing valuable comments. The stack traces encountered at each step helped me point in the right direction.

ClassDefNotFoundError while class in classpath

I'm trying to run ParSeMiS. According to the documentation, it requires ant, prefuse and antlr jars to be available in its lib directory. I've put all the required jars in it. However, when I try to run it, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: antlr/TokenStreamException
at de.parsemis.miner.environment.Settings.parseFileName(Settings.java:198)
at de.parsemis.miner.environment.Settings.parseOption(Settings.java:312)
at de.parsemis.miner.environment.Settings.parse(Settings.java:170)
at de.parsemis.miner.environment.Settings.parse(Settings.java:122)
at de.parsemis.Miner.run(Miner.java:358)
at de.parsemis.Miner.main(Miner.java:61)
Caused by: java.lang.ClassNotFoundException: antlr.TokenStreamException
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 6 more
Now, I've verified that antlr/TokenStreamException.class is present in the antlr jar. I have tried adding the jar manually to the classpath by both exporting the CLASSPATH variable and setting it via the -cp switch. However, none of that works, and I still get this exception. Can anybody help me figure out what's wrong? Thanks.
The problem is that you're using -jar which ignores your CLASSPATH environment variable. You should list your dependencies in the manifest, as shown here, e.g.
Class-Path: lib/ant.jar lib/antlr-3.4-complete.jar lib/prefuse.jar
(It should have still worked with an explicit -cp option, however. My guess is that you got something wrong when specifying that, and assumed it was the same underlying cause as the failure when using the environment variable.)
I realize this question is very old, but I just had exactly the same problem and found this thread. For posterity, I'm posting how I did eventually get it to run:
As mentioned above, when running with -jar, java apparently ignores the class path. So don't run it with -jar. instead include the jars in the path and run the class directly. Poking around, the following should work (paths are on my Ubuntu 12.10 system):
java -cp /usr/share/java/antlr.jar:/full/path/to/parsemis.jar de.parsemis.Miner
You can then pass in options to the above. Maye sure you use full paths, and no shortcuts like ~/foo, as they apparently don't expand.
Of course, if you're using a Dot-formatted graph like I am, it dies very early on complaining of "unexpected char 0xA", but at least it gets further.

Categories

Resources