Java Forum / General / July 2006
Help with -classpath and packages
dlarsson - 27 Jun 2006 04:48 GMT 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: derek_larsson@comcast.net
dlarsson - 27 Jun 2006 04:54 GMT This should be so simple .. but I am really baffled why I can't get this to work ....
- Derek
EMail: derek_larsson@comcast.net
Sigmund Hansen - 27 Jun 2006 17:03 GMT > 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: [quoted text clipped - 8 lines] > 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) [quoted text clipped - 7 lines] > 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
Fergus Gibson - 28 Jun 2006 11:24 GMT > 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...
Oliver Wong - 29 Jun 2006 20:23 GMT [crossposted to comp.lang.java.programmer, because apparently the comp.lang.java newsgroup is deprecated]
> 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 [quoted text clipped - 5 lines] > 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
Sigmund Hansen - 30 Jun 2006 00:40 GMT > 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 [quoted text clipped - 5 lines] > 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)...
Fergus Gibson - 30 Jun 2006 04:29 GMT > 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...
Oliver Wong - 03 Jul 2006 15:07 GMT > 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 [quoted text clipped - 5 lines] > 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
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|