• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java packages question

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So - what's the code to make this work ? I simply can't be that far off.

Edwin
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Put a "package subdir" statement in mathh.java .

2) Remove "subdir" from your classpath.

That should do it.
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic