Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java packages question

 
Edwin Davidson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Edwin Davidson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Put a "package subdir" statement in mathh.java .

2) Remove "subdir" from your classpath.

That should do it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic