wood burning stoves 2.0*
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
Author

java packages question

Edwin Davidson
Greenhorn

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

I need a small hand understanding packages. This is mainproj.java 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);
System.out.println(answer);
}
}
-----------------------------------------------

below is mathh.java 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\mainprog.java:12: cannot access subdir.mathh
bad class file: .\subdir\mathh.java
file does not contain class subdir.mathh
Please remove or make sure it appears in the correct subdirectory of the
classpath.
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 mathh.java ?

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

Thanks.
Edwin
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


42
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
Greenhorn

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

Edwin
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

1) Put a "package subdir" statement in mathh.java .

2) Remove "subdir" from your classpath.

That should do it.


[Jess in Action][AskingGoodQuestions]
 
GeeCON Prague 2014
 
subject: java packages question