Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

package vs non-pacakged classes : java.lang.NoClassDefFoundError:

Thread view: 
Steve - 24 Feb 2006 18:34 GMT
Hi;

It has been a while since I have done anything with stand alone java
classes from the command line.  I have gotten that dreaded
java.lang.NoClassDefFoundError.

I made a simple HelloWorld class ( not part of a package ) in
C:\home\Projects\exp

I used these commands in a bat file to run it:

-----------------------------------------------------------------
@echo off

SET CLASSPATH=.
echo ClASSPATH =  %CLASSPATH%
echo
echo Running java HelloWorld
echo
java -Xmx1024m HelloWorld
-------------------------------------------------------------------

It ran fine.

However, when I recompiled the class to have the package statement
"package helloworld" I got the NoClassDefFound error.

I tried putting a copy of the HelloWorld class and executing it from
either of these directories:
C:\home\Projects\exp
C:\home\Projects\exp\helloworld

I also modified my bat file to be like this
-----------------------------------------------------------
@echo off

SET CLASSPATH=.;C:\home\Projects\exp\helloworld
echo ClASSPATH =  %CLASSPATH%
echo
echo Running java helloworld.HelloWorld
echo
java -Xmx1024m helloword.HelloWorld
------------------------------------------------------------------

I could not get the class to run.

Any ideas or help would be appreciated.

Thanks much in advance

Steve
Rhino - 24 Feb 2006 21:39 GMT
> Hi;
>
[quoted text clipped - 27 lines]
> C:\home\Projects\exp
> C:\home\Projects\exp\helloworld

You need to recompile the HellowWorld class after you add the package
statement then try to execute the revised version from
C:\home\Projects\exp\helloworld. If you simply moved the version the class
that didn't have the package name in it to the helloworld directory, that is
not going to work.

> I also modified my bat file to be like this
> -----------------------------------------------------------
[quoted text clipped - 11 lines]
>
> Any ideas or help would be appreciated.

The last line from the (second) batch file is clearly not going to work if
you've copied it accurately: the package name in the batch file is
"helloword", not "helloworld".

Try making the changes I suggested first; then, if it still doesn't work,
post back and I'll see if I can help. I'm just going off the top of my head
but I think one or both suggestions will help you. If not, I'll concoct my
own example and document what works for you. I've bumped into this problem a
few times over the years and I've been meaning to document the right answers
for ages....

--
Rhino
Fred Kleinschmidt - 24 Feb 2006 22:25 GMT
> Hi;
>
[quoted text clipped - 38 lines]
> echo
> java -Xmx1024m helloword.HelloWorld
Assuming you mena helloworld.HelloWorld, there is no such thing in your
CLASSPATH.
If instead you use
SET CLASSPATH=.;C:\home\Projects\exp
then place HelloWorld.class in C:\home\Projects\exp\helloworld is should
work.

When you have a package name "x.y.z", and you run
  java x.y.z.SomeClass

it searches for SomeClass.class in directories A\x\y\z,  B\x\y\z, ... etc.,
where A, B, etc. are in the CLASSPATH

Signature

Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project

Paul Hamaker - 25 Feb 2006 09:54 GMT
Another way to put it, is to say, that the classpath is a collection of
starting-points, dirs or jars, where each contains directory-branches
or paths corresponding to package-names. Copy rt.jar to something.zip
and look inside, then you'll notice paths like java.lang, java.awt etc.
each containing their respective classes.
---------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com
Steve - 05 Mar 2006 11:50 GMT
Thanks for the help guys.

It looks like my bad spelling has gotten me again :)

Steve


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.