"kj0" <k...@mailcity.com> wrote in message news:9f36lq$nnl$1...@panix3.panix.com...
:
: I have a class called Tiny:
:
: package com.acme;
: public class Tiny {
: public static void main (String args[]) {
: Tiny instance = new Tiny();
: }
: }
:
: ...that compiles fine:
:
: % javac Tiny.java
: % ls
: Tiny.class Tiny.java
:
: ...but doesn't run:
:
: % java Tiny
: Exception in thread "main" java.lang.NoClassDefFoundError: Tiny (wrong
name: com/acme/Tiny)
: at java.lang.ClassLoader.defineClass0(Native Method)
: at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
: at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:111)
: at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
: at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
: at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
: at java.security.AccessController.doPrivileged(Native Method)
: at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
: at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
: at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286)
: at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
: at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
:
: My CLASSPATH variable is
:
: % echo $CLASSPATH
: ./:/usr/local/jdk1.3.1/htmlconv1_3/converter/classes
:
: (I'm working on Linux/csh.)
:
: Of course, it runs fine once I remove the package declaration.
:
: I'm trying to find the full, comprehensive lowdown on packages, with
: no luck. I've looked everywhere I can think of (javac man page, Sun's
: docs, The Java Programming Language, The Java Tutorial, and several
: other Java books) for the answer to this problem, but I have found
: zilch.
:
: Where's the straight dope on packages?
:
: Thanks!
:
: KJ
> Where's the straight dope on packages?
See http://www.pobox.com/~skeet/java/compiling.html
--
Jon Skeet - sk...@pobox.com
http://www.pobox.com/~skeet
If replying to the group, please don't mail me at the same time
The exception did try to indicate that the class name was wrong.
If the JVM still throws a NoClassDefFoundError, then make sure that the
directory containing the 'com' directory is in your CLASSPATH.
kj0 wrote:
> I have a class called Tiny:
>
> package com.acme;
> public class Tiny {
<SNIP>
> }
>
> ...that compiles fine:
<SNIP>
> ...but doesn't run:
>
> % java Tiny
> Exception in thread "main" java.lang.NoClassDefFoundError: Tiny (wrong name: com/acme/Tiny)
<SNIP>
--
Ciao,
Michel
LiveNet Information Solutions, Inc.
http://www.liven.com/
The way he compiled it, there *isn't* any com directory, otherwise it
wouldn't have found Tiny.class at all.
<snip classic problem>
The page
http://www.yoda.arachsys.com/java/compiling.html
is a good start.
Jim S.
> Thank you all.
>
> The page http://www.yoda.arachsys.com/java/compiling.html was
> particularly helpful.
Excellent - any further suggestions are most welcome.
> The following worked:
>
> % javac -d . Tiny.java
> % java com.acme.Tiny
> %
Right - but I still recommend that you keep Tiny.java in com/acme as
well, otherwise you'll rapidly lose track of what's going on.
> Does anybody know where exactly Sun has the official rules of
> compiling and running packages?
The language spec will tell you about how classes are named, what
packages actually are, etc. The tools documentation for javac will tell
you a bit more about what happens when you compile.
Just to expand on this a bit:
Packages and the requirements for compiling pacakges are two separate
things. As Jon said, to learn about packages, read the relevant portions
of the language specification. How you compile packages has nothing to
do with the Java language. It is strictly an implementation detail
associated with the Java system you use (the javac compiler in your
case).
Other systems may have different requirements.
Jim S.