I’m about porting a linux tool to windows. The tool works fine on linux system, but now on windows I get this “cannot find symbol” error.
I have this little main class:
package foo; import foo.bar; public class Main { public static void main(String[] args) throws Exception { bar.getInstance(); } }
and the error appears now, while doing javac Main.java:
import foo.bar: cannot find symbol ^ symbol: class bar location: package foo
Main.java and bar.java are in the same directory. what am I missing?
Advertisement
Answer
For one thing, bar
should be called Bar
to be idiomatic…
Ideally, you should compile from the directory above Main.java, like this:
javac -d out foo/Main.java foo/Bar.java
That will create a directory called “out” containing another directory “foo”, which will contain Main.class and Bar.class. So from the parent directory again, you could run:
java -cp out foo.Main
The source locations don’t have to match the package structure. You could just call javac from the directory containing Main.java
and Bar.java
like this:
javac -cp out Main.java Bar.java
(And then run it in the same way as before) However, it’s generally a much better idea to structure your source code according to packages.
You may well find it easier to use an IDE (Eclipse or NetBeans, for example) which will handle all the compilation etc for you. If you do want to build a real project from the command line, you should probably look into using a full build system such as Ant or Maven.
(Note that you’d get the same error on Linux as on Windows, if you tried to compile in the same way.)