File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Basic Command Line Classpath Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic Command Line Classpath Problem" Watch "Basic Command Line Classpath Problem" New topic

Basic Command Line Classpath Problem

Dan Falcone

Joined: Feb 03, 2009
Posts: 8
Hello - I have a real mental block about classpaths and packages. Can someone please set me straight?

I have a layout manager at
~ / JavaStuff / LAYOUT_MANAGERS /
which looks like
I compile it successfully from ~ / JavaStuff / LAYOUT_MANAGERS with
javac -d bin/com/danf ./
so that the layout manager class file ends up as
~ / JavaStuff / LAYOUT_MANAGERS / bin / com / danf / testLayout / Test01LayoutManager.class
Good so far.
I also have a source file that calls this layout manager at
~ / JavaStuff / TEST /
which looks likeI compile it with
javac -d bin -classpath "~/JavaStuff/LAYOUT_MANAGERS/bin" ./
and it produces this error: cannot access com.danf.testLayout.Test01LayoutManager
bad class file: ~/JavaStuff/LAYOUT_MANAGERS/bin/com/danf/testLayout/Test01LayoutManager.class
class file contains wrong class: testLayout.Test01LayoutManager
Please remove or make sure it appears in the correct subdirectory of the classpath.

where line 8 isI apologize ahead of time if this is embarrassingly obvious. I didn't want to clutter the post with the entire programs since I thought this was enough to go on. I guess what I really need (beyond the scope of this post) is a simple primer about classpaths, packages, and import statements.

Thanks very much for any attention you can give this.

- Dan
Narendira Sarma

Joined: Nov 14, 2008
Posts: 18
Your package declaration in Test01LayoutManager should be like

Dan Falcone

Joined: Feb 03, 2009
Posts: 8
Thanks Narendira, but when I do that and recompile it the resulting layout manager class ends up farther down in ~ / JavaStuff / LAYOUT_MANAGERS / bin / com / danf / com / danf / testLayout / Test01LayoutManager.class.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

The directory structure of your source files and compiled class files must match the package hierarchy that you're using. So if your class is in package com.danf.testLayout, it should be in a directory com/danf/testLayout.

Note that in you do "package testLayout;" but in you have "import com.danf.testLayout.Test01LayoutManager;". Those two are not in accordance with each other.

See Creating and Using Packages in Sun's Java Tutorials.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Dan Falcone

Joined: Feb 03, 2009
Posts: 8
Thanks very much Jesper - My thinking was that compiling the layout manager placed the class at
~ / JavaStuff / LAYOUT_MANAGERS / bin / com / danf / testLayout / Test01LayoutManager.class

Then when compiling the test source with classpath
~ / JavaStuff / LAYOUT_MANAGERS / bin

the remaining part (com / danf / testLayout / Test01LayoutManager.class) would be resolved by the import statement
import com.danf.testLayout.Test01LayoutManager;

Your comment about the package and import statements being in accordance must be key though - I was assuming that as long as combining the classpath with the import statement gave the same path as the location of the class, that was enough.

Off now to read the Sun Java Tutorial (which I have in the past but obviously need a refresher).

Thanks again...

- Dan

I agree. Here's the link:
subject: Basic Command Line Classpath Problem
It's not a secret anymore!