aspose file tools*
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
Author

Basic Command Line Classpath Problem

Dan Falcone
Greenhorn

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 / Test01LayoutManager.java
which looks like
I compile it successfully from ~ / JavaStuff / LAYOUT_MANAGERS with
javac -d bin/com/danf ./Test01LayoutManager.java
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 / Test01Harness.java
which looks likeI compile it with
javac -d bin -classpath "~/JavaStuff/LAYOUT_MANAGERS/bin" ./Test01Harness.java
and it produces this error:
Test01Harness.java:8: 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
Greenhorn

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

Dan Falcone
Greenhorn

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: 14104
    
  16

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 Test01LayoutManager.java you do "package testLayout;" but in Test01Harness.java 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 7 API documentation
Scala Notes - My blog about Scala
Dan Falcone
Greenhorn

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

 
wood burning stoves
 
subject: Basic Command Line Classpath Problem