Java - 如何使用类文件? [英] Java - How do I use a class file?

查看:136
本文介绍了Java - 如何使用类文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,我想知道如何将类文件导入netbeans并使用它。

I'm new to Java and am wondering about how to import class files into netbeans and use it.

我理解类文件是机器可读的字节码,但我不在乎发生了什么。我只是想将它导入到我当前的项目,并已经识别它,所以我可以使用该类。

I understand that a class file is machine-readable byte code but I don't care what's going on under the hood. I'd just like to import it into my current project and have it recognize it so I can use the class.

此外,类文件嵌入在JAR文件。我将JAR文件导入项目窗口中的库文件夹/选项卡,但我不知道如何让我的项目来识别类。

Also, the class file is embedded within a JAR file. I imported the JAR file into my libraries folder/tab in the projects window but I don't know how to get my project to recognize the class. It says "cannot find symbol" whenever I try to instantiate an object.

推荐答案

您必须通过调用源名称来导入包。即 import hello.Car; 。在你的情况下,你试图调用导入JAR文件夹名称,导致一个错误无法找到符号。

You have to import by calling the name of source package . ie import hello.Car; . In your case you are trying to call import on JAR folder name which leads to an error "cannot find symbol" .

让我尝试给出一个更好理解的例子。

Let me try to give an example for better understandability


  • 考虑这个简单的Vehicle应用程序,它有Car类和Test Car类

  • Consider this simple Vehicle application which has Car class and Test Car class

将它转换为jar并将其添加到另一个名为ImportVehicleJar的项目中

Convert it into jar and add it into another project called ImportVehicleJar


  • 要在Main.Java文件中实例化Car类,如下图所示

希望这有助于!

这篇关于Java - 如何使用类文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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