Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Packages And Running Code

0 views
Skip to first unread message

Jason Teagle

unread,
Jul 10, 2002, 6:12:47 AM7/10/02
to
Until yesterday, I was not using packages; just straightforward classes. So
once I had compiled the various classes, I could navigate to the directory
where they had been put, then type

"c:\programming\java 2.0\bin\java.exe" -cp .;
JPCC
"D:\My Work\Java\JPCC\programming\JPCC\JPCC.java"

(lines separated for clarity)

and off it toddled quite happily.

Last night I started trying to use packages... I duly put

package programming.JPCC ;

at the start of each file, and compiled. It created a programming\JPCC dir
structure below the current directory - fine.

Now, if I navigate to that lower JPCC directory and try my favourite line to
run it, it says:

D:\My Work\Java\JPCC\programming\JPCC>"c:\programming\java
2.0\bin\java.exe" -cp
.; JPCC "D:\My Work\Java\JPCC\programming\JPCC\JPCC.java"

Exception in thread "main" java.lang.NoClassDefFoundError: JPCC (wrong name:
pro
gramming/JPCC/JPCC)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:11
1)
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)


The odd thing is, I swear it worked the first time last night, but it won't
work now. I just removed the packages from the code again, and it runs
fine - so I'm obviously doing something wrong with packages, but I don't
know what.

I also tried going up to the level above programming\JPCC to try it from
there, but then of course it gave the standard ClassDefNotFound error
because there was no JPCC.class in that directory.

Did I miss something?


--
--
Jason Teagle
ja...@teagster.co.uk
-----------------------------------------------------------
A list of programming resources I use:
ML: www.windev.org, www.codecipher.com, www.beginthread.com
MB: www.codeguru.com, www.codeproject.com
NG: comp.lang.java.*
OI: www.php.net
-----------------------------------------------------------

Jon Skeet

unread,
Jul 10, 2002, 6:38:20 AM7/10/02
to
Jason Teagle <ja...@teagster.co.uk> wrote:
> Until yesterday, I was not using packages; just straightforward classes. So
> once I had compiled the various classes, I could navigate to the directory
> where they had been put, then type
>
> "c:\programming\java 2.0\bin\java.exe" -cp .;
> JPCC
> "D:\My Work\Java\JPCC\programming\JPCC\JPCC.java"
>
> (lines separated for clarity)
>
> and off it toddled quite happily.
>
> Last night I started trying to use packages... I duly put
>
> package programming.JPCC ;

Just to check you realise: that breaks Sun's naming conventions. I know
you may not like them, but I thought I'd check you realised. I've also
seen people have problems with classes having the same name as packages
(as is in fact the case in your example) - this will never happen if you
follow Sun's conventions.



> at the start of each file, and compiled. It created a programming\JPCC dir
> structure below the current directory - fine.
>
> Now, if I navigate to that lower JPCC directory and try my favourite line to
> run it, it says:
>
> D:\My Work\Java\JPCC\programming\JPCC>"c:\programming\java
> 2.0\bin\java.exe" -cp
> .; JPCC "D:\My Work\Java\JPCC\programming\JPCC\JPCC.java"

That's your mistake. You need to go back up to the top-level directory
and run

java -cp . programming.JPCC.JPCC

> Did I miss something?

You missed that the classname you give to the JRE has to be a fully-
qualified classname.

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Jason Teagle

unread,
Jul 10, 2002, 7:09:31 AM7/10/02
to

"Jon Skeet" <sk...@pobox.com> wrote in message
news:MPG.17961ef9b...@dnews.peramon.com...
> Jason Teagle <ja...@teagster.co.uk> wrote:


> That's your mistake. You need to go back up to the top-level directory
> and run
>
> java -cp . programming.JPCC.JPCC
>
> > Did I miss something?
>
> You missed that the classname you give to the JRE has to be a fully-
> qualified classname.

* bangs head on table *

You know, I tried

java -cp .; programming.JPCC

from that dir... so I almost got it right. Now I think about it, of course -
how would it know what class in that package was the one to run? Guess I was
getting frustrated and lacked concentration.

Once again, you solved my problem and I'm happy - thanks!

0 new messages