Java 中的编译接口

发布于 2024-12-08 21:18:13 字数 2626 浏览 0 评论 0原文

我刚刚发现了这一点,我希望有人知道发生了什么事。要遵循的命令(在临时目录中运行它,事先清除所有内容):

没有问题:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩套路吗 2024-12-15 21:18:13

是的,我们开始吧:
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.

自演自醉 2024-12-15 21:18:13

编译 Myclass.java 时添加 classpath 选项

javac -classpath . Myclass.java

While compiling Myclass.java add the classpath option

javac -classpath . Myclass.java
童话 2024-12-15 21:18:13

情况 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 the javac will look for Mypackage/Myface.class. Thats not there. You should compile with javac -d . which makes javac to create the appropriate directory structure for classes it generates. That is also implicitely in the searchpath for the next javac call.

Case 3: When you compile Myclass.java it will look for Mypackage/Myface.class, then for a suitable source file given on the commandline then for a suitable sourcefile in Mypackage/Myface.java. Neither exist. Either compile with javac -d Myface.java Myclass.java or shove the *.java into the appropriate directory structure indicated by the package names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文