aspose file tools*
The moose likes Beginning Java and the fly likes Head First Servlets: Can't run test class on BeerExpert servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Servlets: Can Watch "Head First Servlets: Can New topic
Author

Head First Servlets: Can't run test class on BeerExpert servlet

Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

Hello,

I'm in Chapter 3 of Basham et al "Head First Servlets & JSP". Per the instructions on page 82 (this is the 2004 edition if there are more editions out there), I created a test file for the BeerExpert model. Well, I'm using the coffee example, so my test class is named TestCoffeeExpert. I followed the directory structure outlined in the book, so my test class is located in
C:/MyProjects/coffeeV1/src/com/example/model.

Here is what the class looks like:



I don't get any errors when I compile it using cmd. I've compiled it using a simple "javac TestCoffeeExpert.java" in the directory (command prompt looks like . I've also tried compiling using from the coffeeV1 directory using the -d option as stated in the book. No problems compiling. But when I try to run the class using "java TestCoffeeExpert", I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: TestCoffeeExpert2 (wrong name: com/example/model/TestCoffeeExpert2).

Again, I've tried running the java command from different directories, adding the necessary directory information before the name, but to no avail. I've created simple "HelloWorld" classes, in this same directory and others, and used the different javac options; I was able to run them just fine. The only difference was that I didn't put the package name or import statements in the HelloWorld classes. Is that my problem?

Amanda


Sun Certified Programmer for the Java 2 Platform 1.4
Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

By the way, I know the error message says "TestCoffeeExpert2" and my class is named TestCoffeeExpert. That's not the problem as I was trying multiple versions of TestCoffeeExpert .
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The java command expects the *fully qualified classname* of your class, which includes the package name as a prefix. That's what it's complaining about: it finds a class-file of the correct name, but the class itself has a different name than the one you specified at the command line.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
I just did all that tutorial in Chapter 3 and I work out fine, I stick to the Beer but anyways that's not the point. My beer model code is not exactly the same as the book as I added more conditional statement for different response on the Beer selection(sorry to digress). I did notice that your coffee expert code has main method in it, The book example DID NOT. I believe the model code was not meant to be tested by using Java command, it was meant to be tested by the Servlet version 2 you mod after you write the model code. The servelt version 2 is suppose to call getBrand();. Hope this helps.
Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

Originally posted by Ilja Preuss:
The java command expects the *fully qualified classname* of your class, which includes the package name as a prefix. That's what it's complaining about: it finds a class-file of the correct name, but the class itself has a different name than the one you specified at the command line.


Thanks Ilja. So what command prompt should I use to run my "javac" and "java" commands and how should they look?

I tried running



then



Since I knew the class file wasn't there because it was put in the "classes" directory with the above listed "javac" command, I also tried



But that didn't work either.

So I tried compiling and running the java class from its source directory:



and then

.

Nothing worked .

This is a really good exercise for me because I'm used to using IDEs and it handles this stuff for me. This will make me a better programmer, I know, but it's frustrating to me that I'm not getting it .

Amanda
[ August 12, 2007: Message edited by: Amanda Albert ]
Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

Originally posted by Davie Lin:
I did notice that your coffee expert code has main method in it, The book example DID NOT. I believe the model code was not meant to be tested by using Java command, it was meant to be tested by the Servlet version 2 you mod after you write the model code. The servelt version 2 is suppose to call getBrand();. Hope this helps.


Thanks for your response David!

I'm following the instructions above where the authors provided the code for the BeerExpert class. There's a section called "Build the test class for the model" and they do not provide any code for that. That section also states that "it's just like any other Java class, and you can test it without Tomcat." I took this to mean adding a main method so I could just run it through a command line.

For now, I've created another directory under C:\MyProjects\coffeeV1 called "testSource". I added the package details up to "example", deciding against dividing the classes up in "web" and "model". So the java source and the compiled class for the test classes will all be in the same directory. I also took out the package declaration from the test class and I was able to compile and run it just fine.

But I would like to see how it's supposed to work with the package declaration, and with the class in a different directory than the java source.

Thanks again!

Amanda
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Unfortunately, the error message of the JVM is a little bit misleading - the fully qualified name of a class uses dots for separating package fragments, not slashes. So the command line for running the program should be

java com.example.model.TestCoffeeExpert [args]

Notice that the parent folder of the com package (in your case the classes folder, it seems) needs to be in the classpath.
Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

Originally posted by Ilja Preuss:
Unfortunately, the error message of the JVM is a little bit misleading - the fully qualified name of a class uses dots for separating package fragments, not slashes. So the command line for running the program should be

java com.example.model.TestCoffeeExpert [args]

Notice that the parent folder of the com package (in your case the classes folder, it seems) needs to be in the classpath.


By "classpath" do you mean the CLASSPATH environment variable which I set for my computer, or the command line prompt so that I type what you wrote at the

C:\MyProjects\coffeeV1\classes >

command line prompt (this is the directory structure to get to the compiled class?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Amanda Albert:


By "classpath" do you mean the CLASSPATH environment variable which I set for my computer, or the command line prompt so that I type what you wrote at the

C:\MyProjects\coffeeV1\classes >

command line prompt (this is the directory structure to get to the compiled class?


The latter will work if the environment variable contains the "." folder. Another approach is to use the -cp command line switch, for example:

C:\MyProjects\coffeeV1> java -cp classes com.example.model.TestCoffeeExpert [args]

All three approaches are equivalent, which on you use mostly is a matter of personal preference.
Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

Originally posted by Ilja Preuss:


The latter will work if the environment variable contains the "." folder. Another approach is to use the -cp command line switch, for example:

C:\MyProjects\coffeeV1> java -cp classes com.example.model.TestCoffeeExpert [args]

All three approaches are equivalent, which on you use mostly is a matter of personal preference.


Thanks Ilja! I'll try running your example and see what happens. Do you know what the directory structure should look like if test classes will be included in a project?

Amanda
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Head First Servlets: Can't run test class on BeerExpert servlet