javac“找不到符号”命令行出错 [英] javac "cannot find symbol" error with command line
问题描述
我有两个班级Owning和OwningAccessor。这些文件位于同一目录中。
I have two classes Owning and OwningAccessor. The files are in the same directory.
public class Owning {
String _name = "";
public void printBanner()
{
}
public void printOwning(double amount)
{
printBanner();
//print details
System.out.println("name:" + _name);
System.out.println("amount:" + amount);
}
}
public class OwningAccessor {
public void access()
{
Owning o = new Owning();
o.printOwning(500);
}
}
当我尝试编译拥有
,我收到了编译错误。 javac -cp的OwningAccessor
。 OwningAccessor.java
When I tried to compile OwningAccessor
with javac -cp . OwningAccessor.java
, I got compilation error.
symbol : class Owning
location: class smcho.OwningAccessor
Owning o = new Owning();
^
OwningAccessor.java:6: cannot find symbol
symbol : class Owning
location: class smcho.OwningAccessor
Owning o = new Owning();
^
这有什么问题?代码在eclipse IDE下编译得很好。
What's wrong with this? The code compiles fine under eclipse IDE.
推荐答案
好的,我们假设你的代码分布在文件中,如下所示
Ok, let's suppose you have the code distributed in files as follows
myproject
├── out
└── src
├── OwningAccessor.java
└── Owning.java
转到命令提示符,将目录更改为 myproject的
。一旦发出以下命令:
Go to your command prompt, and change directory to myproject
. Once there issue the following command:
javac -d out -sourcepath src src/OwningAccessor.java
我刚试过它,它运作得很好。您编译的类将位于 out
文件夹中:
I just tested it and it works just fine. Your compiled classes will be located in the out
folder:
.
├── out
│ ├── OwningAccessor.class
│ └── Owning.class
└── src
├── OwningAccessor.java
└── Owning.java
编译一个类将触发所有其他依赖类的编译。编译器将自动在 src
文件夹中查找它们。
Compiling one class will trigger the compilation of all other dependent classes. The compiler will automatically look for them in the src
folder.
这篇关于javac“找不到符号”命令行出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!