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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help in CLASSPATH" Watch "Need help in CLASSPATH" New topic
Author

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 MyMath.java 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 UrMath.java 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();
System.out.println(ob.callMe());
}
}

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

javac -d . MyMath.java UrMath.java

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

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

javac -classpath . TempClass.java

I got the error:

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

Now, when I changed the TempClass.java 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 MyMath.java 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??
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Need help in CLASSPATH