Ok really stupid question, but I have some methods defined in a file called x.java, which is in the default folder, and in the same folder, I have a file called z.java. I want to use the functions defined in x in z. When I try, it tells me the function is undefined. I tried to put import x.java; but it says x.java cannot be resolved. What am I missing here?
-
2Check your classpath. Does it include the current directory? i.e the "."Suresh Kumar– Suresh Kumar2010-10-06 03:25:46 +00:00Commented Oct 6, 2010 at 3:25
-
I was able to successfully use x class's method in z.java without any import. My method in x.java has default access and I have created object for x class and called the method. Probably class path problem as Suresh Kumar mentioned.Reddy– Reddy2010-10-06 04:48:29 +00:00Commented Oct 6, 2010 at 4:48
-
issue this command set classpath=.;%classpath% in windowsReddy– Reddy2010-10-06 04:48:56 +00:00Commented Oct 6, 2010 at 4:48
Add a comment
|
3 Answers
Based on your description, I'd bet there's a good chance both of your source files defined classes in the default package (i.e., you don't explicitly define a package for them).
You can't import a class that's in the default package.
Recommend you put your class x in a named package (e.g., foo.bar.x), then you can import it:
import foo.bar.x;
1 Comment
Reddy
I don't know why he accepted this answer (though it is very good answer), it probably problem with classpath.