简体   繁体   English

Java - 如何使用类文件?

[英]Java - How do I use a class file?

I'm new to Java and am wondering about how to import class files into netbeans and use it. 我是Java的新手,我想知道如何将类文件导入netbeans并使用它。

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. 我只想将它导入到我当前的项目中并让它识别它以便我可以使用该类。

Also, the class file is embedded within a JAR file. 此外,类文件嵌入在JAR文件中。 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. 我将JAR文件导入到项目窗口中的库文件夹/选项卡中,但我不知道如何让我的项目识别该类。 It says "cannot find symbol" whenever I try to instantiate an object. 每当我尝试实例化一个对象时,它都会说“找不到符号”。

You have to import by calling the name of source package . 您必须通过调用源包的名称导入。 ie import hello.Car; import hello.Car; . In your case you are trying to call import on JAR folder name which leads to an error "cannot find symbol" . 在您的情况下,您尝试在JAR文件夹名称上调用导入,这会导致错误“无法找到符号”。

Let me try to give an example for better understandability 让我试着举一个更好理解的例子

  • Consider this simple Vehicle application which has Car class and Test Car class 考虑这个简单的Vehicle应用程序,它具有Car类和Test Car类 替代文字

  • Convert it into jar and add it into another project called ImportVehicleJar 将其转换为jar并将其添加到另一个名为ImportVehicleJar的项目中

    alt text http://i46.tinypic.com/qxmlxt.png alt text http://i46.tinypic.com/qxmlxt.png

    • In order to instantiate the Car class in Main.Java file add the following as shown in figure 为了在Main.Java文件中实例化Car类,添加以下内容,如图所示 替代文字

    Hope this helps !! 希望这可以帮助 !!

In Netbeans (version 5.5.1), you can add a jar file to a project by right clicking the project name, and choosing Properties, then Libraries (from Categories), and there is an "Add JAR/Folder" button, which can be used for adding it to the compile-time and/or run-time classpath. 在Netbeans(版本5.5.1)中,您可以通过右键单击项目名称,选择“属性”,然后选择“库”(来自“类别”),然后选择“添加JAR /文件夹”按钮,将jar文件添加到项目中。用于将其添加到编译时和/或运行时类路径。 Adding it to the Compile-time Libraries is well enough, because it will automatically added to the run-time through its "Classpath for Compiling Sources" entry. 将它添加到编译时库就足够了,因为它会通过“编译源类路径”条目自动添加到运行时。

You either need to specify the full package pathname to the class. 您需要指定该类的完整包路径名。 eg 例如

com.foobar.acme.Clobula myBigClobula = new com.foobar.acme.Clobula();

or use an import statement 或使用进口声明

import com.foobar.acme.Clobula
...
.
 Clobula myBigClobula = new Clobula();

Also, class files aren't machine-readable / binary in the sense that compiled c files are. 此外,在编译的c文件的意义上,类文件不是机器可读/二进制文件。 Open a class file with your text editor and you will see that it is in fact a list of text instructions. 使用文本编辑器打开一个类文件,您将看到它实际上是一个文本指令列表。 The Java Virtual Machine looks at these class files and interprets them, executing the resulting instructions. Java虚拟机查看这些类文件并解释它们,执行生成的指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM