By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,130 Members | 2,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,130 IT Pros & Developers. It's quick & easy.

Help with -classpath and packages

P: n/a

Okay folks, I thought I was doing something very, very simple,
but I cannot seem to get this to work at all.

Can anyone identify what I am doing wrong here-?
_________________________________
1. I have a class called "Car" that defines a package with
the statement: package com.example.rentalcar;

2. Then I have a test program called TestCar that imports
this package as follows:
import com.example.rentalcar.*;

3. Now, I then first compile the Car package as follows
javac -classpath . -d . Car.java

This succesfully compiles Car.java and places the Car.class file
in the subdirectory ==> com/example/rentalcar relative to
my current working directory (where both source files originate
from).

So, I have a current directory and then the generated
com/example/rentalcar subdirectory with the "Car.class"
file placed there - by this compile statement.
So far so good.

4. Okay, now I want to compile the little test program
that has the corresponding import com.example.rentalcar.*;
statement in it and uses the "Car" class.

---
So I try this:

javac -classpath . TestCar.java

I get the error:
TestCar.java:8: cannot access Car
bad class file: .\Car.java
file does not contain class Car
Please remove or make sure it appears in the correct
subdirectory of the classpath.
---

If I try to use the fully qualified path to my current directory

javac -classpath C:\Derek\java TestCar.java

I still get the same error:
TestCar.java:8: cannot access Car
bad class file: .\Car.java
file does not contain class Car
Please remove or make sure it appears in the correct
subdirectory of the classpath.
---

Now if I try to use the fully qualified path to the package itself (I
know this shouldn't work)

javac -classpath C:\Derek\java\com\example\rentalcar TestCar.java

I get the error:
TestCar.java:2: package com.example.rentalcar does not exist
import com.example.rentalcar.*;
TestCar.java:8: cannot access Car
bad class file: C:\Derek\java\com\example\rentalcar\Car.class
class file contains wrong class: com.example.rentalcar.Car

---

Just what do I need to do-?

What the heck do I have to set classpath to in order to get the java
compilier to resolve the import com.example.rentalcar.* statement
properly
in this TestCar program-?

I can't seem to feed anything to -classpath to make it work.....

What did I do wrong (and how do compile TestCar) -?
I appreciate all helpful responses .....
- Derek

EMail: de***********@comcast.net


Jun 27 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

This should be so simple .. but I am
really baffled why I can't get this to work ....

- Derek

EMail: de***********@comcast.net

Jun 27 '06 #2

P: n/a
dlarsson wrote:
Okay folks, I thought I was doing something very, very simple,
but I cannot seem to get this to work at all.

Can anyone identify what I am doing wrong here-?
*snip*

---
So I try this:

javac -classpath . TestCar.java

I get the error:
TestCar.java:8: cannot access Car
bad class file: .\Car.java
file does not contain class Car
Please remove or make sure it appears in the correct
subdirectory of the classpath.
---
Well, the Car.java file should really be placed inside a directory
called com/example/rentalcar
This is why it tells you to place it in the correct subdirectory,
or at least I think it is.
Usually you'd have a structure sort of like this:

projectdir/src/com/example/rentalcar/Car.java

projectdir/build/com/example/rentalcar/Car.class

When compiling from the src dir, just set the -d to ../build (backslash
on windows, obviously).
If this doesn't work then I don't know,
I'd recommend you use NetBeans or Eclipse to do your projects though, it
would simplify these tasks a lot.
Now if I try to use the fully qualified path to the package itself (I
know this shouldn't work)

javac -classpath C:\Derek\java\com\example\rentalcar TestCar.java

I get the error:
TestCar.java:2: package com.example.rentalcar does not exist
import com.example.rentalcar.*;
TestCar.java:8: cannot access Car
bad class file: C:\Derek\java\com\example\rentalcar\Car.class
class file contains wrong class: com.example.rentalcar.Car


Obviously because there's no subdirectory com/example/rentalcar
Jun 27 '06 #3

P: n/a
Sigmund Hansen wrote:
Well, the Car.java file should really be placed inside a directory
called com/example/rentalcar
This is why it tells you to place it in the correct subdirectory,
or at least I think it is.
Yes, Sigmund, I think you've got it. The compiler can't find the Car.java
file where it expects it in order to ensure that it's being used properly by
TestCar.java.

Derek, it's important to conceive of the -cp and -d switches as referring to
the root of a directory tree. When you're using packages, you need to have
a full path representing the package (com/example/rentalcar/) as Sigmund has
said. Java is organized hierarchially, and it applies this to both its
source file and its compiled files.

I'd recommend you use NetBeans or Eclipse to do your projects though,
it would simplify these tasks a lot.


I tried to get into using an IDE, but I never could. I don't like how they
put each project into its own directory. I use a text editor and two
hierarchies, one under "source" and one under "classes". This way I can
import anything from anywhere in my tree with a simple import statement,
which I could find any easy way to do in an IDE due to the class and source
files being distributed into many different project dirs.

I always felt like I was missing something when I tried to use them, like
there had to be some easy way to do it...
Jun 28 '06 #4

P: n/a
[crossposted to comp.lang.java.programmer, because apparently the
comp.lang.java newsgroup is deprecated]

"Fergus Gibson" <no****@noemail.com> wrote in message
news:TNsog.103723$Mn5.10920@pd7tw3no...

I tried to get into using an IDE, but I never could. I don't like how
they put each project into its own directory. I use a text editor and two
hierarchies, one under "source" and one under "classes". This way I can
import anything from anywhere in my tree with a simple import statement,
which I could find any easy way to do in an IDE due to the class and
source files being distributed into many different project dirs.

I always felt like I was missing something when I tried to use them, like
there had to be some easy way to do it...


I guess what you're missing is that generally, you don't want to have
circular dependencies between projects going all over the place.

For example, you might have one project for each application and/or
library, and the applications won't directly be accessing each other's class
files. If they communicate in any way, it might be via some sort of plugin
architecture (e.g. one project is a plugin for another project which is the
"main" application). These application projects might have references to a
library project, but the library would never need to know or access any
class files defined within the applications which use that library.

- Oliver

Jun 29 '06 #5

P: n/a
Fergus Gibson wrote:
I tried to get into using an IDE, but I never could. I don't like how they
put each project into its own directory. I use a text editor and two
hierarchies, one under "source" and one under "classes". This way I can
import anything from anywhere in my tree with a simple import statement,
which I could find any easy way to do in an IDE due to the class and source
files being distributed into many different project dirs.

I always felt like I was missing something when I tried to use them, like
there had to be some easy way to do it...


Ah, yeah.
For libraries that you create (I for one am making a game library, with
common classes that I will be using among several libraries),
you can add to your list of libraries, then add those libraries to
projects that use them,
if you change something in the library, when compiling a project that
uses it, it will also recompile the library project.

At least that's how it works in NetBeans, I've used Sun Java Studio
(later Sun ONE Studio for Java or something, and now NetBeans), and I
really like it, I hear that Eclipse is better, but NetBeans was always
free, and I didn't really have much use for anything more fancy. ;)

In NetBeans I simply go into the project's properties, then to Libraries
(it doesn't really have to be libraries, but any other package really),
then I just click Add Project, and all imports are fixed.
Still, everyone has a preferred way of working, and I only recently have
had use for this(as I don't really code a lot), and found it very easy
to set up, now I even have version control in place, with my own
subversion server (if you want to use svn I recommend that you use NB
5.5beta over NB 5.0 - way better, although the svn support is not
perfectly bug free nor completely finished)...
Jun 29 '06 #6

P: n/a
Sigmund Hansen wrote:
For libraries that you create (I for one am making a game library,
with common classes that I will be using among several libraries),
you can add to your list of libraries, then add those libraries to
projects that use them,
if you change something in the library, when compiling a project that
uses it, it will also recompile the library project.
This term "library" is a big part of my confusion. There is no concept of a
"library" intrinsic to Java. I don't know what the IDE implies when it
calls this thing a "library" and that thing an "application". Java has
packages and classes, which I understand quite well.

I know what the generic terminology means, but I don't know what the IDE
means when it uses those words. What is a Java library? A utility package
whose classes are meant to be used by many other packages ("applications")
to achieve some common objectives?

I guess I feel a certain resistance to learning the IDE. I understand the
language and packages and classes, and I don't want to learn a whole new set
of semantics. I also balk at becoming totally dependent on the IDE. It
seems to me there would be no easy to way to compile the "applications" and
"libraries" with just the simple javac command-line compiler once they are
split up into a whole bunch of isolated directory trees.

At least that's how it works in NetBeans, I've used Sun Java Studio
(later Sun ONE Studio for Java or something, and now NetBeans), and I
really like it, I hear that Eclipse is better, but NetBeans was always
free, and I didn't really have much use for anything more fancy. ;)


I used Sun ONE Studio for Java or whatever exactly it was called and
NetBeans too. I found them off-putting. I guess I want an IDE the uses Java
semantics and approaches rather than trying to look, act, and sound like
Visual Studio...
Jun 30 '06 #7

P: n/a

"Fergus Gibson" <no****@noemail.comwrote in message
news:OU0pg.109799$Mn5.2326@pd7tw3no...
>
This term "library" is a big part of my confusion. There is no concept of
a "library" intrinsic to Java. I don't know what the IDE implies when it
calls this thing a "library" and that thing an "application". Java has
packages and classes, which I understand quite well.

I know what the generic terminology means, but I don't know what the IDE
means when it uses those words. What is a Java library? A utility
package whose classes are meant to be used by many other packages
("applications") to achieve some common objectives?
I don't think the IDE applies any special behaviour to what one might
call a "library" versus what one might call an "application".

I guess the idea is that each project will eventually be distributed as
its own JARs. Some of these JARs have an entry point, and so when you try to
run them (via "java -jar whatever.jar"), something will actually happen.
These are "applications". Other jars just contain classes for applications
to use, and you can't actually "run" them. Those would be the libraries.

But again, you don't mark a project as being an application or a library
within Eclipse (I don't know about the other IDEs). It's just a label I
apply when speaking to other humans to communicate the intent of the
project.
>
I guess I feel a certain resistance to learning the IDE. I understand the
language and packages and classes, and I don't want to learn a whole new
set of semantics. I also balk at becoming totally dependent on the IDE.
It seems to me there would be no easy to way to compile the "applications"
and "libraries" with just the simple javac command-line compiler once they
are split up into a whole bunch of isolated directory trees.
If you're working from the command line, I think you'd usually use a
build tool like "make" or "ant".

- Oliver

Jul 3 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.