Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

Straight dope on packages?

瀏覽次數:0 次
跳到第一則未讀訊息

Yohan

未讀,
2001年5月30日 中午12:36:252001/5/30
收件者:
try java com.acme.Tiny
packages are a way of organizing your classes into meaning ful groups.

"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


Jon Skeet

未讀,
2001年5月30日 中午12:45:192001/5/30
收件者:
kj0 <k...@mailcity.com> wrote:

> 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

dalal

未讀,
2001年5月30日 中午12:49:562001/5/30
收件者:
To run a class on the command line, it needs to include the package name.
i.e.:
% java com.acme.Tiny

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/

Jon Skeet

未讀,
2001年5月30日 下午1:04:342001/5/30
收件者:
dalal <da...@my-deja.com> wrote:
> To run a class on the command line, it needs to include the package name.
> i.e.:
> % java com.acme.Tiny
>
> 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.

The way he compiled it, there *isn't* any com directory, otherwise it
wouldn't have found Tiny.class at all.

Jim Sculley

未讀,
2001年5月30日 中午12:59:392001/5/30
收件者:
kj0 wrote:
>
> I have a class called Tiny:

<snip classic problem>

The page

http://www.yoda.arachsys.com/java/compiling.html

is a good start.


Jim S.

Jon Skeet

未讀,
2001年5月30日 下午2:48:552001/5/30
收件者:
kj0 <k...@mailcity.com> wrote:

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

Jim Sculley

未讀,
2001年5月31日 上午10:38:342001/5/31
收件者:
Jon Skeet wrote:
>
> kj0 <k...@mailcity.com> wrote:
>
> > 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.

0 則新訊息