473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help with -classpath and packages


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
7 17850

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
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
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
[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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.