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:-
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
posted
0
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: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.