Forums Register Login

BeerExpert NoClassFoundDefError Issue (NOT COMPILE ERROR) - HF Servlets & JSP

+Pie Number of slices to send: Send
I can compile fine (I notice others are having that issue) and just want to run my test class simply by using "java TestClass" (not deploying in Apache "yet"), but I get the NoClassDefFoundError issue. Below is the compile statements, classpath variable, BeerExpert.java, and TestClass.java. I imagine I need to either modify my classpath statement or run from a different directory. Thank you in advance for your assistance!

C:\myprojects\beerv1>echo %classpath%
C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\Apache Software F
oundation\Tomcat 5.5\common\lib\servlet-api.jar;C:\myprojects;C:\myprojects\beer
v1\classes;

C:\myprojects\beerv1>javac -d classes src/com/example/model/BeerExpert.java <-- creates BeerExpert.class in classes/com/example/model

C:\myprojects\beerv1>javac -d classes src/com/example/web/TestClass.java <-- creates TestClass.class in classes/com/example/web

C:\myprojects\beerv1>java TestClass
Exception in thread "main" java.lang.NoClassDefFoundError: TestClass (wrong name
: com/example/web/TestClass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: TestClass. Program will exit.


TestClass.java




BeerExpert.java


+Pie Number of slices to send: Send
Welcome to JavaRanch

I ought to get a copy of that book, in view of the number of people who get problems with BeerExpert. But have a look at the CLASSPATH you have printed out. If you have a CLASSPATH which has been set (in this case it appears to have been set by QT), you ought to have a . for "current directory" in. I suggest you edit your system CLASSPATH to include the dot. More details about CLASSPATH here. You should end up with something like where it says "Windows 95/98/ME". Note the . and ; in that example.

The code you have quoted has package declarations; have you tried with the package declarations (and the import statement) deleted? That might be the simplest way to run it, unless you specifically want to use the packages.

I tried the code you quoted and it ran and compiled all right. Note I don't have a system classpath on this machine, so the java tool will assume the classpath means "current directory".

[Campbell@localhost ~]$ cd java
[Campbell@localhost java]$ mkdir beer
[Campbell@localhost java]$ cd beer
[Campbell@localhost beer]$ gedit BeerExpert.java
[Campbell@localhost beer]$ gedit TestClass.java
[Campbell@localhost beer]$ javac -d . BeerExpert.java
[Campbell@localhost beer]$ javac -d . TestClass.java
[Campbell@localhost beer]$ java com.example.web.TestClass
string yo
booya
[Campbell@localhost beer]$
. . .
[Campbell@localhost beer]$ echo $classpath

[Campbell@localhost beer]$

Note that you have to use the fully-qualified name for the class you are invoking: like this: java com.example.web.TestClass and you navigate to the directory one above "com". I'm not using Windows, but that shouldn't make any difference.

Try emulating that lot, and see whether it works. Come back and tell us if you still have problems, and good luck with it

By the way: I edited your post to add code tags; see how much better it looks.
+Pie Number of slices to send: Send
Haha...funny. The book is actually quite good - I think they are testing us with this little Beer Expert tutorial. You mention the "." to go in the CLASSPATH. I am on XP and did do this and it worked! So I thank you for that....and of course for cleaning up my very first post - you rock Campbell thanks!
+Pie Number of slices to send: Send
You're welcome

The idea of testing people is to see whether they have understood the problem. You will probably notice a difference between the code in the book and what you download from the website: the downloaded code has more package declarations and import statements. And there are some examples with misprints in the book and the downloaded version corrected.
+Pie Number of slices to send: Send
Actually I am going to have to re-tract my statement as it did NOT work. The only way I can get it to run is if I remove the package declaration in TestClass (package com.example.web). This then compiles TestClass to C:\myprojects\beerv1\classes instead of C:\myprojects\beerv1\classes\com\example\web where I originally intended to run and place as in the package declaration.

So now I am back to the beginning. Why do I need to remove my package declaration and how can I get it to run from C:\myprojects\beerv1\classes\com\example\web ?
+Pie Number of slices to send: Send
Why have you got a "classes" directory? Are you using NetBeans?

Navigate to the classes directory, then invoke the fully-qualified name of the class

java com.example.web.TestClass

Have a look at these Java™ Tutorial sections about packages: 1 2. See whether they are helpful.
+Pie Number of slices to send: Send
I think what I need to do is go back to Java 101....but thats what happens when you get on a .NET project at work for the last 3 years.

Anyway, I was actually just trying to play around and call a class from one package to another. But now that I am refreshing my memory (and correct me if I am wrong), I should really only put classes into packages and not be executing (or placing) a program from within a package (?)

+Pie Number of slices to send: Send
Not quite sure I understand the last point, but you usually execute a Java application, with package names, from outside the package folders. So I think, yes.
+Pie Number of slices to send: Send
Exactly...we are on the same page. Thanks again!
+Pie Number of slices to send: Send
You're welcome
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1431 times.
Similar Threads
servlet compiling errors
javac compile import package BeerSelect.java:3: error: package com.example.model does not exist
HFSJ page 85
Package does not exist
Package doesn't exists - error!!- How to resolve?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:30:31.