i've started learning java i'm having few problems related noclassdeffounderror
. i've tried research solutions site , elsewhere still haven't been able solve them. run programs windows 7 command prompt classpath environment variable set instead of defining each program run. it's set c:
. troubleshoot, created test package simplified directory structure.
in c:\test
, have shape.java
, circle.java
. shape.java
looks this:
package test; class shape { void draw() { system.out.println("drawing new shape"); } }
and circle.java
looks this:
package test; public class circle extends shape { public static void main(string[] args) { circle round = new circle(); round.draw(); } }
if compile both shape.java
, circle.java
c:\
in command prompt operations like
c:\>javac test\shape.java c:\>javac test\circle.java
and run compiled circle.class
file like
c:\java test\circle
i output expect:
drawing new shape
however, if compile shape.java
, circle.java
inside c:\test
like
c:\test>javac shape.java c:\test>javac circle.java
and try run circle.class
anywhere like
c:\test>java circle
or this
c:\> java test\circle
i following error message.
exception in thread "main" java.lang.noclassdeffounderror: circle (wrong name: t est/circle) @ java.lang.classloader.defineclass1(native method) @ java.lang.classloader.defineclass(classloader.java:792) @ java.security.secureclassloader.defineclass(secureclassloader.java:14 2) @ java.net.urlclassloader.defineclass(urlclassloader.java:449) @ java.net.urlclassloader.access$100(urlclassloader.java:71) @ java.net.urlclassloader$1.run(urlclassloader.java:361) @ 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) @ sun.launcher.launcherhelper.checkandloadmain(launcherhelper.java:482)
could explain why java files compile within c:\test
folder compiled circle file produces error? there wrong classpath? reading , forthcoming solutions!
edit: i've done bit more testing , turns out if compile shape.java
, circle.java
files c:\test
directory run c:\
directory, works. change anything?
edit 2: after more testing, turns out can in fact compile , run shape.java , circle.java files within c:\test directory long use command java test.circle. misunderstood way in jvm looks classes. understand uses same method compiler when analyses package import statements.
you can anywhere on system, long have classpath bounded in .
only java vm able scan classpath, , locate class test.circle
in package test
.
so, correct execution java test.circle
(always), , classpath needs point have \test\circle.class
available. check circle.class
expect it.
Comments
Post a Comment