aspose file tools
The moose likes Beginning Java and the fly likes Need help in CLASSPATH Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help in CLASSPATH" Watch "Need help in CLASSPATH" New topic

Need help in CLASSPATH

Bharat Roy
Ranch Hand

Joined: Jul 01, 2004
Posts: 156
hi everyone,

i am having a strange problem regarding package/classpath.
Very Sorry, Its a long post but please help.

I did like this:

I created file in C:\Java , the contents were:

package com.anbh.Maths;

public class MyMath {
public static int add(int x, int y) {
return x + y;

public static int sub(int x, int y) {
return x-y;

Created file again in C:\Java, contents were :

package com.anbh.Maths;

public class UrMath {
public String callMe() {
return "Inside CallMe()";

Created file TempClass again in C:\Java, Contents were

import com.anbh.Maths.*;

public class TempClass {
public static void main(String[] args) {
System.out.println(MyMath.add(-1, -1));
UrMath ob = new UrMath();

Then I compiled it like below: (My current directory was C:\Java)

javac -d .

This Complied successfully and created directories C:\Java\com\anbh\Maths.

Then I tried to compile like: (My current directory was C:\Java)

javac -classpath .

I got the error: cannot access MyMath
bad class file: .\
file does not contain class MyMath
Please remove or make sure it appears in the correct subdirectory of the classpa
System.out.println(MyMath.add(-1, -1));
1 error

Now, when I changed the file's import statement as:-

import com.anbh.Maths.MyMath;
import com.anbh.Maths.UrMath;

i.e. I named every class individually, EVERYTHING WORKED FINE.

Why import com.anbh.Maths.* notation is not working???

Please help, I had wasted 1 day on this.

Thanks in advance.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I'm moving this to Java in General (Beginner).

SCJP Tipline, etc.
Daniel Rhoades
Ranch Hand

Joined: Jun 30, 2004
Posts: 186
Ah, move your java source files (except TempClass) outside the working directory or move them to their package locations, the import statement is mistakingly picking them up.

For example should live in com/anbh/Maths/

I'm not 100% sure why the compiler requires this... as I would of thought class files have precedence over source files.
[ July 06, 2004: Message edited by: Daniel Rhoades ]

Drinking more tea is the key...
Bharat Roy
Ranch Hand

Joined: Jul 01, 2004
Posts: 156
Thank you very much. But I still do not get the reason why I am supposed to move the source files into the package directory??
I agree. Here's the link:
subject: Need help in CLASSPATH