• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classpath

 
Tom Sullivan
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been having a real problem with getting code to compile from the Head First Servlets book. I am able to get the servlets themselves to compile as the when I compile by setting the classpath a follows:

C:\LearningProjects\beerV1\src\com\example\web>javac -classpath %CATALINA_HOME%\common\lib\servlet-api.jar BeerSelect.java

Using the -d flag, I also got the BeerSelect class to go to the right place, c:\LearningProjects\beerV1\classes\com\example\web.

The problem I am having is in testing the BeerSelect class. In case you are not familiar with the stellar Head First series and its teaching code (do I get something for that flattery), it follows:



This class clearly does not require any servlet api functionality so compiling it was pretty straight forward. I compiled it with the -d flag and put the class file in the directory listed above (except in the model rather than the web node). In keeping with the iterative approach, I built a test class. It looks like this:



When I compile, I get the BeerExpert not found exception. How can this be? They are both in the same package. In any event, I have tried any number of incantations to overcome this problem. I want to solve my regular classpath problems once and for all. I suspect that this is just one of those. Here is the directory structure that I am using.

c:\LearningProjects\beerV1\src\com\example\model (javac from here)
c:\LearningProjects\beerV1\classes\com\example\model (put classes here and look for BeerExpert.class here).

Can you just walk me through this so that I can get it to compile and hopefully after doing that I will finally get it. I have gone through many web pages and books trying to figure this out. I think that I am doing what I am supposed to but my results are not good so I know I am doing something wrong.

Thanks,
Tom
 
todd runstein
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried not using the -classpath argument? Also, while compiling the test class, have you tried:
C:\LearningProjects\beerV1\src\com\example\web>javac -classpath .;%CATALINA_HOME%\common\lib\servlet-api.jar TestBeerSelect.java
(notice the ".;" which means "here and %CATALINA_HOME\common\....)

Classpaths are the killer hurdle for newbies.
 
Aaron Ting
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings Tom,

I think your problem seems to occur as you have not imported the "BeerExpert" .class file into your program..

Seems like you missed out the "import com.example.model.BeerExpert" statement in your "TestBeerExpert" java class..

I think it should be fine after you have put in that statement..

Do let me know if i am wrong with that..


P.S.: I think it would be great if you start using an IDE, e.g. JCreator, with simple functionality as it definitely aids a java-starter like me during a few years back.
[ December 20, 2005: Message edited by: Aaron Ting ]
 
Tom Sullivan
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice.

I did not try the -classpath option (although I did when I complied the servlet). I will give that a go next. I thought that since I was not needing any servlet api support for this that there would be no reason to point to the servlet api.

I did not import the BeerExpert.class file because I also thought that having both classes in the same package would preclude my needing to do that.

I concur that IDE's help a lot. When I have been building real-world systems, I have been using Eclipse. I have found learning Java to be challenging but not spectacularly hard. However, anytime that I need to work with a new api or define the class path, I wind up spending hours and even days trying to figure it out. I know that this should not be hard but for whatever reason, it seems to be a real problem for me. The book specifically instructs to not use an IDE which is why I have not for these exercises.

I will try the solutions you guys gave me and report back. Thanks so much!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic