Junit in eclipse produces NoClassDefFoundError when using junit - java

I am new to jUnit and unit testing in general, and have been trying to set up my testing environment and failing all day. This is trying to test a method in my android application.
I added a test class to the same package the java class is in. I added a method as below:
#Test
public void testIsExpectedNote() {
assertTrue(Frequency.isExpectedNote(440, "A"));
}//should return true
This fails with the following stack trace:
java.lang.NoClassDefFoundError: android/util/Log
at dataLogicLayer.Frequency.isExpectedNote(Frequency.java:13)
at dataLogicLayer.FrequencyTest.testIsExpectedNote(FrequencyTest.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
...
Caused by: java.lang.ClassNotFoundException: android.util.Log
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 25 more
Any ideas what is going wrong? I have been playing with my run configurations and in the classpath i have:
jre system library [jre7]
junit 4

Do you have the android sdk in your classpath? search for the jar containing the class android.util.Log in your classpath
regards
Grub

The answer was to make a new project and do testing in there instead, must make it an android junit test, and make sure the run configs are correct. thanks to Grub for your help

Seems like you are mixing Android SDK and Java SDK. You should not be doing that. Android is a self sufficient library.

Related

Flink: NoClassDefFoundError when runnig a Table API query

I have a streaming Table API query and want to write the resulting Table into a .csv file using the table.writetoSink(sink) method.
When I run the program, I got the following error.
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/commons/compiler/CompileException
at org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery(SqlToRelConverter.java:580)
at org.apache.flink.api.table.FlinkPlannerImpl.rel(FlinkPlannerImpl.scala:102)
at org.apache.flink.api.table.BatchTableEnvironment.sql(BatchTableEnvironment.scala:132)
at table_streaming_test.main(table_streaming_test.java:59)
Caused by: java.lang.ClassNotFoundException: org.codehaus.commons.compiler.CompileException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 4 more
I have also included the commons-compiler.jar file in my project.
Flink's Table and SQL API is built on Apache Calcite which depends on commons-compiler from org.codehaus.janino.
The error message suggests that the commons-compiler dependency is not included in your application. One approach is to built a fat jar containing all dependencies of your application. Flink's quickstart Maven architypes (Java and Scala) provide a blueprint for that. Another approach is to add all required dependencies to the ./lib folder of your Flink setup.

Successfully implementing external classes in Eclipse. (Java)

I have been trying to use this external class folder which was given from my programming lecturer a few months back. I followed people's explanations of how to add via "configure build class", and the application somewhat recognises the class because a shortlist of some of the methods are given. It also does not return any errors prior to runtime but once it's ran I get this:
Exception in thread "main" java.lang.NoClassDefFoundError: Console
at lab3.q2.main(q2.java:15)
Caused by: java.lang.ClassNotFoundException: Console
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
This is really frustrating as it works from the linux computers in my college without issue. The class file is just a variation of the scanner that we use for user i/o, I have it attached.
Console.class file

error while running main function without libgdx

I have been using libGdx for a while now. I have created a new project without using libGdx and created some class trying to practice some code if it works as I intended. However, I'm getting error that main class is not found in the libgdx package.
How Do I switch between libgdx and just native IDE.
So for example...
package com.example.example;
public class Example
{
public static void main (String[] args)
{
System.out.println("hey");
}
}
when i try to run this code I get error
Exception in thread "main" java.lang.NoClassDefFoundError: com/badlogic/gdx/jnigen/BuildTarget$TargetOs
at com.badlogic.gdx.physics.box2d.utils.Box2DBuild.main(Box2DBuild.java:13)
Caused by: java.lang.ClassNotFoundException: com.badlogic.gdx.jnigen.BuildTarget$TargetOs
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
How do I fix this? Thanks in advance!
I ran into this error today and Lestat was correct.
In your IDE (in my case Eclipse): go to Run> Run As> Java application.

Java3D - Some classes not found but classpath is set correctly

I have a class that uses Java3D that successfully compiles, although when I try to run it it says: Exception in thread "main" java.lang.NoClassDefFoundError:
javax/media/j3d/Canvas3D,
With the following stack trace:
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.media.j3d.Canvas3D
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Stuff I have already tried:
I couldn't even get it to compile until I copied the Java3D jars into /jre/lib/ext/.
I have the latest Java, Java3D and I am using Notepad++ and I have tried reinstalling Java3D.
I have set the classpath/path correctly.
There are no errors in the code.
I can run normal Java applications I have created.
I have tried using the classpath thing when running the application (java -classpath blahblah ApplicationName).
I have checked inside the jars and the classes (including Canvas3D) are definitely in there.
Not everything you say is true.
IF the classes are in the jars, and IF you have set the classpath correctly, THEN you would not get this message.
I suspect you have a build path confused with a classpath (which is mostly associated with runtime, where build path is associated with compile time). Without knowing more details of your runtime environment, it's hard to be more specific.
It always puzzles me how someone with a problem they cannot solve asserts so definitely that all these things are set up "correctly": how do you know? Isn't the problem some evidence to the contrary, at least enough for a small doubt?

Napkin custom LookAndFeel in Eclipse

Premise
I'm pretty new to GUI programming but I'm looking for a way to put the Napkin Look and feel onto my application though Eclipse.
What I've done thus far
I've had a look around and from what I can see I need to put the napkin .jar's into my buildpath. I've tried to do this by right-clicking in the navigator and selecting import --> File system and highlighting the .jars associated with Napkin.
Inside of my Main I've added the following code:
try{
UIManager.setLookAndFeel("net.sourceforge.napkinlaf.NapkinLookAndFeel");
}catch(Exception e){
e.printStackTrace();
}
Which I believe should apply the Napkin Look and Feel, unfortunately, I keep getting this error:
java.lang.ClassNotFoundException: net.sourceforge.napkinlaf.NapkinLookAndFeel
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at javax.swing.SwingUtilities.loadSystemClass(Unknown Source)
at javax.swing.UIManager.setLookAndFeel(Unknown Source)
at yulfy.alterd.FrameTest.main(FrameTest.java:8)
Question
What on earth am I doing wrong? (I'm sorry if it's terribly obvious!)
Thanks for your time!
Links
Napkin site:
http://napkinlaf.sourceforge.net/
Notes
It seems that the latest is 1.2 and not alpha001 which is the first
file available on Sourceforge.
If any further clarification/information is needed, don't hesitate to
post.
right click over your project, go to build path, click on "add external jar" and select the jar

Categories