javac :编译一个使用其他类的 .java 文件 [英] javac : Compiling a .java file which uses other classes in it

查看:35
本文介绍了javac :编译一个使用其他类的 .java 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有 3 个 java 文件

HI i have 3 java files

a.java  
b.java  
c.java  

我设法使用

javac example/a.java  
javac example/b.java  

但是当我对 c.java 做同样的事情时,我得到了错误错误:找不到符号 b 和 c

but when i do the same for c.java I get the error error: cannot find symbol b and c

关于我如何解决这个问题的任何建议?

Any suggestions on how i could solve this problem ?

所有的java文件都在同一个文件夹中

All the java files are in the same folder

推荐答案

当您尝试编译 class ab>c.这允许编译器验证它们是否存在,找出它们具有哪些方法等.

You have to have classes a and b in your classpath when you try to compile class c. This allows the compiler to verify that they exist, figure out what methods they have, etc.

javac 对包名和类路径非常敏感.最简单的方法是像这样同时编译所有三个 javac example/a.java example/b.java example/c.java.

javac is pretty sensitive to package names and classpaths. The easiest thing to do is to compile all three at the same time like so javac example/a.java example/b.java example/c.java.

如果你进入example的父目录(我们称之为src),那么你可以运行以下命令:

If you go to the parent directory of example (let's call it src), then you can run the following:

javac -cp src src/example/c.java

您必须这样做的原因是因为您的类将它们的包列为example.由于您的包名称,javac 正在其类路径中寻找 example 目录,它希望在其中找到 a.classb.class.

The reason you have to do it this way is because your classes have their packages listed as example. Because of your package name, javac is looking for the example directory in its classpath, where it expects to find a.class and b.class.

这篇关于javac :编译一个使用其他类的 .java 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆