• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compiling in a development environment

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I've bought HF's Servlet & JSP book and am going through the tutorial in chapter three. It shows a structure for the development environment where we should keep our code while developing.

At one point the book has a 'model' class and asks you to build a test class for the model. So I've done this, and given it a package statement to put it in the same package as the model class. In my directory, both src files are in the appropriate directory in the development environment ie src\com\example\model.

I compiled the model class by:



and no problem, the class file appears in the classes part of the development environment.

The issue is when I try and compile my TestBeerExpert class it can't find (or access) the BeerExpert class file. I get a 'can't find symbol' message saying that it can't find the BeerExpert class.

In the command promt, I am starting in the beerV1 folder and entering:



I have also tried starting from the folder that the BeerExpert class is in and entering something like:



I don't think it is an access issue because I have put exactly the same package statement at the top of both - package com.example.model; so as they're in the same package they should be able to see each other. So I'm stuck and would appreciate it very much if someone could point out my mistake.

Thanks in advance.

Joe
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add "-cp classes" to your compiler flags.
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

Thanks very much I can compile it using the -cp tag.

Can I ask you another question? I am now just trying to use the 'java' command to run the TestBeerExpert, but I keep on getting a NoClassDefFound error. I have tried;
Being in the classes\com\example\model directory and using - 'java TestBeerExpert'.
Being in the Beer-V1 directory and using - 'java -cp classes TestBeerExpert'.
Being in the Beer-V1 directory and using - 'java -cp classes com\example\model\TestBeerExpert'.
Being in the Beer-V1\classes directory and using - 'java com\example\model\TestBeerExpert'.
and numerous other combinations.

I don't really know what I'm doing though. Both class files are in the Beer-V1\classes\com\example\model folder. Which directory should I have cd'd to in the command prompt? Do I need to use the -cp command?

Many thanks

Joe
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe Lemmer wrote:Being in the Beer-V1 directory and using - 'java -cp classes com\example\model\TestBeerExpert'.
Being in the Beer-V1\classes directory and using - 'java com\example\model\TestBeerExpert'.


You came sooooo close with these two...

The java command takes a (fully qualified) class name, not a file name. So if you replace the backslashes with dots in the two above commands (i.e. com.example.model.TestBeerExpert), both should work (provided TestBeerExpert is in package com.example.model).
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brilliant! Thank you very much Rob!

I was reading this post by Campbell Ritchie et al and was still not able to compile. But what I was doing was using %>java com.example.model.TestBeerExpert from the beerV1 folder rather than the classes folder.

So, if I understand correctly I should navigate to the folder where all the dependant class files branch off from (ie where their package statements lead directly to the files) which in this case is my classes directory and then type in 'java + fully qualified class name'.

Thanks again



Joe


 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can use the -cp / -classpath flag to explicitly set the root path of your classes.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe Lemmer wrote:I was reading this post by Campbell Ritchie et al . . .

Never believe anything he says.
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sweet!

Thanks for your help Rob.

 
We're all out of roofs. But we still have tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic