Java 中的编译接口
我刚刚发现了这一点,我希望有人知道发生了什么事。要遵循的命令(在临时目录中运行它,事先清除所有内容):
没有问题:
cat << EOF > Myface.java
public interface Myface {}
EOF
cat << EOF > Myclass.java
public class Myclass implements Myface {}
EOF
javac Myface.java
javac Myclass.java
Myclass 不会编译:
cat << EOF > Myface.java
package Mypackage;
public interface Myface {}
EOF
cat << EOF > Myclass.java
package Mypackage;
public class Myclass implements Myface {}
EOF
javac Myface.java
javac Myclass.java
Myclass 不会编译:
cat << EOF > Myface.java
package Mypackage;
public interface Myface {}
EOF
cat << EOF > Myclass.java
package Mypackage;
public class Myclass implements Myface {}
EOF
javac Myclass.java
这里发生了什么?
编辑:所有命令,运行:
javatest@l-bel-smai-ubuntu:~$ ls
javatest@l-bel-smai-ubuntu:~$ pwd
/home/javatest
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myface.java
> public interface Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myclass.java
> public class Myclass implements Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ javac Myface.java
javatest@l-bel-smai-ubuntu:~$ javac Myclass.java
javatest@l-bel-smai-ubuntu:~$ ls
Myclass.class Myclass.java Myface.class Myface.java
javatest@l-bel-smai-ubuntu:~$ rm *
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myface.java
> package Mypackage;
> public interface Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myclass.java
> package Mypackage;
> public class Myclass implements Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ javac Myface.java
javatest@l-bel-smai-ubuntu:~$ javac Myclass.java
Myclass.java:2: cannot find symbol
symbol: class Myface
public class Myclass implements Myface {}
^
1 error
javatest@l-bel-smai-ubuntu:~$ rm *
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myface.java
> package Mypackage;
> public interface Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myclass.java
> package Mypackage;
> public class Myclass implements Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ javac Myclass.java
Myclass.java:2: cannot find symbol
symbol: class Myface
public class Myclass implements Myface {}
^
1 error
javatest@l-bel-smai-ubuntu:~$
I just found this out and I'm hoping someone has a clue as to what's going on. Commands to follow (run this in a temp directory, clearing everything beforehand):
No problems:
cat << EOF > Myface.java
public interface Myface {}
EOF
cat << EOF > Myclass.java
public class Myclass implements Myface {}
EOF
javac Myface.java
javac Myclass.java
Myclass won't compile:
cat << EOF > Myface.java
package Mypackage;
public interface Myface {}
EOF
cat << EOF > Myclass.java
package Mypackage;
public class Myclass implements Myface {}
EOF
javac Myface.java
javac Myclass.java
Myclass WON'T compile:
cat << EOF > Myface.java
package Mypackage;
public interface Myface {}
EOF
cat << EOF > Myclass.java
package Mypackage;
public class Myclass implements Myface {}
EOF
javac Myclass.java
What's going on here?
EDIT: All the commands, run:
javatest@l-bel-smai-ubuntu:~$ ls
javatest@l-bel-smai-ubuntu:~$ pwd
/home/javatest
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myface.java
> public interface Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myclass.java
> public class Myclass implements Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ javac Myface.java
javatest@l-bel-smai-ubuntu:~$ javac Myclass.java
javatest@l-bel-smai-ubuntu:~$ ls
Myclass.class Myclass.java Myface.class Myface.java
javatest@l-bel-smai-ubuntu:~$ rm *
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myface.java
> package Mypackage;
> public interface Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myclass.java
> package Mypackage;
> public class Myclass implements Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ javac Myface.java
javatest@l-bel-smai-ubuntu:~$ javac Myclass.java
Myclass.java:2: cannot find symbol
symbol: class Myface
public class Myclass implements Myface {}
^
1 error
javatest@l-bel-smai-ubuntu:~$ rm *
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myface.java
> package Mypackage;
> public interface Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myclass.java
> package Mypackage;
> public class Myclass implements Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ javac Myclass.java
Myclass.java:2: cannot find symbol
symbol: class Myface
public class Myclass implements Myface {}
^
1 error
javatest@l-bel-smai-ubuntu:~$
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,我们开始吧:
Java/Class 文件目录结构必须与声明 Java 类的包结构相匹配,编译和类加载才能正常工作。
Yeah here we go:
Java/Class file directory structure must match the package structure the Java classes are declared in for compilation and class loading to work correctly.
编译 Myclass.java 时添加 classpath 选项
While compiling Myclass.java add the classpath option
情况 2:当您编译
Myclass.java
时,javac
将查找Mypackage/Myface.class
。那不在那里。您应该使用 javac -d . 进行编译,这使得 javac 为其生成的类创建适当的目录结构。这也隐含在下一个 javac 调用的搜索路径中。情况 3:当您编译
Myclass.java
时,它将查找Mypackage/Myface.class
,然后在命令行中查找合适的源文件,然后在 <代码>Mypackage/Myface.java。两者都不存在。使用 javac -d Myface.java Myclass.java 进行编译,或者将 *.java 放入包名称指示的适当目录结构中。Case 2: When you compile
Myclass.java
thejavac
will look forMypackage/Myface.class
. Thats not there. You should compile withjavac -d .
which makesjavac
to create the appropriate directory structure for classes it generates. That is also implicitely in the searchpath for the nextjavac
call.Case 3: When you compile
Myclass.java
it will look forMypackage/Myface.class
, then for a suitable source file given on the commandline then for a suitable sourcefile inMypackage/Myface.java
. Neither exist. Either compile withjavac -d Myface.java Myclass.java
or shove the*.java
into the appropriate directory structure indicated by the package names.