Java:类已解决?

发布于 2024-12-25 15:00:27 字数 366 浏览 1 评论 0原文

我希望这个问题不要重复。但在任何地方都找不到答案:

我有一个文件夹,其中包含两个文件,一个是 A.java,另一个是 B.class。

现在在 A.java 中,我试图声明

public class A extends Applet{
...
    B aB;
}

编译器给了我:

B cannot be resolved to a type

我读了很多帖子,说如果文件位于同一文件夹中,我不需要导入。谁能帮我“解决”这个问题? 非常感谢!

-----------解决了! - 请参阅下面的答案------------------

I hope this question is not repeated. But just can't find answer anywhere:

I have ONE folder containing two files one A.java another B.class.

Now in A.java I am trying to declare

public class A extends Applet{
...
    B aB;
}

The compiler gives me:

B cannot be resolved to a type

I read a lot of posts that say if the files are in the same folder, I don't need to import. Could anyone help me to "resolve" this problem?
Thanks much appreciated!

-----------SOLVED! - SEE ANSWER BELOW------------------

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

分開簡單 2025-01-01 15:00:27

.class 文件需要驻留在类路径变量引用的目录中。通常,您将 .java 文件放在一个目录 (src) 中,编译到另一目录 (bin),并将外部 .class 文件放在第三个目录 (lib) 中。命令如下所示:

# compile
javac -sourcepath src -classpath lib -d bin
# run
java -classpath bin:lib A

使用像 eclipse 这样的 IDE 应该会有很大帮助,因为它可以处理大部分任务细节

The .class files need to reside in a directory referenced by the classpath variable. Usually you put your .java files in one directory (src), compile to another directory (bin) and have external .class files in a third directory (lib). The commands will look like this:

# compile
javac -sourcepath src -classpath lib -d bin
# run
java -classpath bin:lib A

Using an IDE like eclipse should help a lot here as it takes care of most of the details

浮华 2025-01-01 15:00:27

您发布的简单案例对我有用。我会检查以下内容:

  • 您确定 B.classA.java 存在于同一文件夹中吗?
  • 您是否从该文件夹运行 javac
  • 您是否在程序中的所有位置都正确输入了类名 B ?这包括大小写,因为 Java 标识符区分大小写。
  • 您的程序中是否有任何 package 声明?如果有,这些都不起作用,因为您只是将所有内容都放入文件夹中,从而隐式使用默认包。

The simple case that you've posted works for me. I'd check the following things:

  • Are you sure that B.class is present in the same folder as A.java?
  • Are you running javac from that folder?
  • Have you typed the class name B correctly everywhere in your program? This includes capitalization, as Java identifiers are case sensitive.
  • Are there any package declarations in your program? If there are, none of this is going to work, since you're implicitly using the default package by just throwing everything into a folder.
兰花执着 2025-01-01 15:00:27

编译器在其类路径中查找 *.class 文件。它只会在相同的源目录中查找 *.java 文件。您需要设置类路径以包含该目录。

或者您可以使用 IDE 来为您设置所有这些,并在此过程中节省大量时间。

The compiler looks for *.class file in its class path. It will only look for *.java files in the same source directories. You need to set the class path to include the directory.

Or you could use an IDE which sets all this up for you and saves a lot time in the process.

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