java import“无法找到符号” [英] java import "cannot find symbol"
问题描述
我是关于将Linux工具移植到Windows。该工具在Linux系统上运行良好,但现在在Windows上我得到这个找不到符号的错误。
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.
我有这个小主要类:
package foo;
import foo.bar;
public class Main {
public static void main(String[] args) throws Exception {
bar.getInstance();
}
}
现在出现错误,同时执行javac Main。 java:
and the error appears now, while doing javac Main.java:
import foo.bar:找不到符号
^
符号:class bar
location:package foo
import foo.bar: cannot find symbol ^ symbol: class bar location: package foo
Main.java和bar.java位于同一目录中。
我缺少什么?
Main.java and bar.java are in the same directory. what am I missing?
推荐答案
首先, bar
应该被称为 Bar
是惯用的...
For one thing, bar
should be called Bar
to be idiomatic...
理想情况下,你应该从目录上面 Main.java,如下所示:
Ideally, you should compile from the directory above Main.java, like this:
javac -d out foo/Main.java foo/Bar.java
这将创建一个名为out的目录,其中包含另一个目录foo,该目录将包含Main .class和Bar.class。所以从父目录再次运行:
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
源位置 以匹配包结构。您可以从包含 Main.java
和 Bar.java
的目录中调用javac这个:
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.
您可能会发现使用IDE(例如Eclipse或NetBeans)更容易,它将为您处理所有编译等。如果你做想要从命令行构建一个真实的项目,你应该考虑使用一个完整的构建系统,如Ant或Maven。
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.
(请注意,如果您尝试以相同的方式编译,则在Linux上会遇到与Windows相同的错误。)
(Note that you'd get the same error on Linux as on Windows, if you tried to compile in the same way.)
这篇关于java import“无法找到符号”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!