• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
  • Himai Minh
  • Carey Brown
  • salvin francis

classpath problems compiling on Windows 2000

Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working through the Heads Up JSPs and Servlets book. I've created a directory structure that looks like this:

I have a class called "BeerExpert.class" located in classes\com\example\model. The first line in the source code is:

"package com.example.model;"

I have a java source file called "BeerSelect.java" located in src\com\example\web.

The relevant import statement is:

"import com.example.model.*;"

I open a command window and change to "c:\MyProjects\BeerV1".

I then attempt to compile BeerSelect.java with this command line:
"javac -d classes src\com\example\web\BeerSelect.java"

and get this error message:

"Package com.example.model does not exist"

Can anyone advise me on what I should be doing differently? I'm sure it must be a problem with paths, but I'm stumped.


EDIT by mw: Added Code Tags to keep indentation of directory structure intact. (Otherwise, it's left-justified.)
[ March 19, 2006: Message edited by: marc weber ]
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to tell the compiler where to find the BeerExpert.class file. But keep in mind it will be looking for the directory that contains com.example.model.BeerExpert.class, not just BeerExpert.class. So you need to tell it to look in the classes directory, not in the classes\com\example\model directory. So at the command line, add a -classpath option to tell the compiler the directory that contains the com.example.model package (i.e. the classes directory).

C:\MyProjects\BeerV1>javac -classpath classes -d classes src\com\example\web\BeerSelect.java

This of course will only work if any and all dependent classes have already been compiled into the classes directory. As an alternative, if you want the compiler to compile any dependent classes it encounters (and all the source code is in the src folder), set your classpath to src, and the compiler will compile both the com.example.web.BeerSelect.java file, and the dependent class com.example.model.BeerExpert (i.e. src\com\example\model\BeerExpert.java), along with any other classes either or those two classes are dependent on (as long as the source code is within the classpath specified, just the src directory in this case).

C:\MyProjects\BeerV1>javac -classpath src -d classes src\com\example\web\BeerSelect.java

Give that a try. Delete all the subdirectories in classes, and run the above compile command. You will see both BeerSelect.class and BeerExpert class appear in their appropriate packages within the classes directory.

As an FYI, in more recent versions of Java, the -cp option is an abbreviation (or alias) for the -classpath option. If memory serves me correct, it was added in version 1.4.2.

I hope that helps.
[ March 19, 2006: Message edited by: Mark Vedder ]
Paper beats rock. Scissors beats tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic