Reputation: 1661
I wrote a very simple program on java stack operation. I am using Java(TM) SE Runtime Environment with OS X 10.9.1. I have tried JDK build 1.7.0_13-b20 and build 1.7.0_51-b13, both do not work for me. I just wanted to test the java Stack class. I do not have a private stack implementation. And the $CLASSPATH environment variable is set to empty.
I tested the same program on Windows (Win 8.1) with JDK 1.7.0_25. It worked fine.
import java.util.*;
public class MyStackTest
{
public static void main(String[] args){
Stack<Integer> mys = new Stack<Integer>();
mys.push(5);
while ( ! mys.empty() ) {
System.out.println(mys.peek());
mys.pop();
}
}
}
However, while compiling using javac 1.7.0_13, I got "cannot find symbol" error:
$ javac MyStackTest.java -Xlint
MyStackTest.java:9: error: cannot find symbol
while ( ! mys.empty() ) {
^
symbol: method empty()
location: variable mys of type Stack<Integer>
1 error
I found that if I change the import statement to
import java.util.Stack
the program compiles fine. Why did "import java.util." cause the problem? How can I tell which class in java.util. cause the problem?
Thanks!
Upvotes: 0
Views: 784
Reputation: 63114
You may have a Stack
implementation in the same package as your MyStackTest
class (in this case the default package name). Comment out everything after new Stack
and print out the class type. If it's not java.util.Stack
then you've found your answer.
import java.util.*;
public class MyStackTest
{
public static void main(String[] args){
Stack<Integer> mys = new Stack<Integer>();
System.out.println(mys.getClass());
}
}
Another possibility is that you have non-UTF8 characters in mys.empty()
. Try deleting that line and hand type it again.
Upvotes: 2
Reputation: 1910
when you run the compiler for execution from the command line (DOS I guess...) it has to be specified the complete route to every class involved in the java file, or every class needs to be in the same package. That's why when you call javac it can't find the Stack class.
Upvotes: 0