exception - How to solve java.lang.NoClassDefFoundError? -


i've tried both example in oracle's java tutorials. both compile fine, @ run-time, both come error:

exception in thread "main" java.lang.noclassdeffounderror: graphics/shapes/square     @ main.main(main.java:7) caused by: java.lang.classnotfoundexception: graphics.shapes.square     @ java.net.urlclassloader$1.run(urlclassloader.java:366)     @ java.net.urlclassloader$1.run(urlclassloader.java:355)     @ java.security.accesscontroller.doprivileged(native method)     @ java.net.urlclassloader.findclass(urlclassloader.java:354)     @ java.lang.classloader.loadclass(classloader.java:424)     @ sun.misc.launcher$appclassloader.loadclass(launcher.java:308)     @ java.lang.classloader.loadclass(classloader.java:357)     ... 1 more 

i think might have main.java file in wrong folder. here directory hierarchy:

graphics ├ main.java ├ shapes |   ├ square.java |   ├ triangle.java ├ linepoint |   ├ line.java |   ├ point.java ├ spaceobjects |   ├ cube.java |   ├ rectprism.java 

and here main.java:

import graphics.shapes.*; import graphics.linepoint.* import graphics.spaceobjects.*;  public class main {     public static void main(string args[]) {         square s = new square(2,3,15);         line l = new line(1,5,2,3);         cube c = new cube(13,32,22);     } } 

what doing wrong here?

update

after put put main class graphics package (i added package graphics; it), set classpath "_test" (folder containing graphics), compiled it, , ran using java graphics.main (from command line), worked.

really late update #2

i wasn't using eclipse (just notepad++ , jdk), , above update solved problem. however, seems many of these answers eclipse , intellij, have similar concepts.

after compile code, end .class files each class in program. these binary files bytecode java interprets execute program. noclassdeffounderror indicates classloader (in case java.net.urlclassloader), responsible dynamically loading classes, cannot find .class file class you're trying use.

your code wouldn't compile if required classes weren't present (unless classes loaded reflection), exception means classpath doesn't include required classes. remember classloader (specifically java.net.urlclassloader) classes in package a.b.c in folder a/b/c/ in each entry in classpath. noclassdeffounderror can indicate you're missing transitive dependency of .jar file you've compiled against , you're trying use.

for example, if had class com.example.foo, after compiling have class file foo.class. example working directory .../project/. class file must placed in .../project/com/example, , set classpath .../project/.

side note: recommend taking advantage of amazing tooling exists java , jvm languages. modern ide's eclipse , idea , build management tools maven or gradle not have worry classpaths (as much) , focus on code! said, this link explains how set classpath when execute on command line.

if class that's missing not project, created this tool looking through open source artifacts given class. paste classname in search box, , it'll find if it's open source.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -