File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Compiling in a development environment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiling in a development environment" Watch "Compiling in a development environment" New topic
Author

Compiling in a development environment

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
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


OCPJP 85%
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Add "-cp classes" to your compiler flags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
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

Joined: Oct 27, 2005
Posts: 19790
    
  20

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

Joined: Oct 24, 2008
Posts: 171
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

Joined: Oct 27, 2005
Posts: 19790
    
  20

Or you can use the -cp / -classpath flag to explicitly set the root path of your classes.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Joe Lemmer wrote:I was reading this post by Campbell Ritchie et al . . .
Never believe anything he says.
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Sweet!

Thanks for your help Rob.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compiling in a development environment