Skip to content
Advertisement

java import “cannot find symbol”

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:

JavaScript

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:

JavaScript

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:

JavaScript

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:

JavaScript

(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.)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement