Two Laptop Bag
The moose likes Beginning Java and the fly likes java packages question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java packages question" Watch "java packages question" New topic

java packages question

Edwin Davidson

Joined: Nov 26, 2003
Posts: 27
Dear all:

I need a small hand understanding packages. This is and is
in e:\justjava2\chap9:

package chap9;

import subdir.*;

class mainprog {

public static void main(String args[]) {
mathh math3=new mathh();
int a=3;
int b=4;
int answer=math3.addd(a,b);

below is and it is in e:\justjava2\chap9\subdir:


public class mathh {

int addd(int x, int y) {
return x+y;


My classpath variable is set to e:\justjava2;.;e:\justjava2\chap9

The compile error I get is:

E:\justjava2\chap9\ cannot access subdir.mathh
bad class file: .\subdir\
file does not contain class subdir.mathh
Please remove or make sure it appears in the correct subdirectory of the
mathh math3=new mathh();
1 error

Tool completed with exit code 1

Can you tell me please what is wrong with this code ? Can I make addd() static instead in ?

Thank you very much. Packages SOUND great in the book but when I try it
things out they just don't work out (sometimes) - I hate that !!

Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
the package structure MUST be reflected both inside the file AND on disk.

According to your source your class mathh isn't in any package yet according to your directory structure it's in chap9.subdir

Jeremy Wilson
Ranch Hand

Joined: Feb 18, 2003
Posts: 166
I would put all my code in a src directory and then compile the code to a bin directory. you could then add the bin directory to your classpath. The package name must follow the heirarchy of the folder structure. This is how classes are located in a package. If you package the classes up into a .jar file, each jar file will need to be added individually on the classpath not just the folder containing the jars.

Jeremy Wilson
Edwin Davidson

Joined: Nov 26, 2003
Posts: 27
So - what's the code to make this work ? I simply can't be that far off.

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

1) Put a "package subdir" statement in .

2) Remove "subdir" from your classpath.

That should do it.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: java packages question
It's not a secret anymore!