Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help in CLASSPATH

 
Bharat Roy
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to Java in General (Beginner).
 
Daniel Rhoades
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bharat Roy
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic