SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
James Tharakan wrote:Because the method in the myTestClass is static , you need to do static imports.
Replace...
import mytestpack.*; by import static mytestpack.*;
Learn about static imports for more details
Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
eileen keeney wrote:Earlier Today I was able to create a package, with a class, that had a method in it, and access the method from another program.
Now I can not even get a very simple case of a package to work, at all.
Even when I go back to the old code that worked, it fails to allow me to compile the code that calls the method in the package.
Code in file myTestClass.java
---
package mytestpack;
import java.io.*;
import java.util.*;
import java.lang.String;
public class myTestClass
{
public static int myTestMethod()
{
System.out.println("hello World");
int i=0;
return i;
}
}
----
I then do this
javac -d . myTestClass.java
It compiles, and creates directory mytestpack
In the directory it places myTestClass.class
Then I write a simple program to call the method.
Code for testpack.java
----
import java.io.*;
import java.util.*;
import java.lang.String;
import mytestpack.*;
class testmypack <-------------------------------------<< ??
{
public static void main(String[] args)
{
int i;
i= myTestClass.myTestMethod();
}
}
---
I attempt to compile the above using this command:
javac testpack.java
when I try to compile the above, I get this error:
testpack.java:11: cannot access myTestClass
bad class file: ./myTestClass.java
file does not contain class myTestClass
Please remove or make sure it appears in the correct subdirectory of the classpath.
i= myTestClass.myTestMethod();
^
Why is it even looking back at myTestClass.java, when I have already successfully compiled the class.
Would it not only need to access the myTestClass.class file?
The path to (and including) mytestpack is in my CLASSPATH.
(besides I get a different error, when it can't find the path).
What baffles me, is that I had this concept working earlier.
I am sure I used the same exact strategy as I show above.
If someone can tell me what I am doing wrong, it would be very much appreciated.
Thanks.
What is testmypack?
John de Michele wrote:Eileen:
I got your program to work by changing the import in the second class from
to
I'm not sure why it works, but it worked for me. BTW, you don't have to do includes for java.lang.* classes, since they are included automatically.
John.
Don't get me started about those stupid light bulbs. |