Java - 如何使用类文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1983513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - How do I use a class file?
提问by ShrimpCrackers
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. 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.
此外,类文件嵌入在 JAR 文件中。我将 JAR 文件导入到项目窗口中的库文件夹/选项卡中,但我不知道如何让我的项目识别该类。每当我尝试实例化一个对象时,它都会说“找不到符号”。
回答by Srinivas M.V.
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" .
您必须通过调用源包的名称来导入。即 import hello.Car;。在您的情况下,您试图在 JAR 文件夹名称上调用导入,这会导致错误“找不到符号”。
Let me try to give an example for better understandability
让我试着举一个例子来更好地理解
Consider this simple Vehicle application which has Car class and Test Car class

Convert it into jar and add it into another project called ImportVehicleJar
alt text http://i46.tinypic.com/qxmlxt.png
Hope this helps !!
将其转换为 jar 并将其添加到另一个名为 ImportVehicleJar 的项目中
替代文字 http://i46.tinypic.com/qxmlxt.png
希望这可以帮助 !!
回答by JuanZe
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. 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.
在 Netbeans(5.5.1 版本)中,可以通过右键单击项目名称,选择 Properties,然后选择 Libraries (from Categories),将 jar 文件添加到项目中,并且有一个“添加 JAR/文件夹”按钮,可以用于将其添加到编译时和/或运行时类路径。将它添加到编译时库就足够了,因为它将通过其“编译源的类路径”条目自动添加到运行时。
回答by President James K. Polk
You either need to specify the full package pathname to the class. e.g
您需要为类指定完整的包路径名。例如
com.foobar.acme.Clobula myBigClobula = new com.foobar.acme.Clobula();
or use an import statement
或使用导入语句
import com.foobar.acme.Clobula
...
.
Clobula myBigClobula = new Clobula();
回答by D.C.
Also, class files aren't machine-readable / binary in the sense that compiled c files are. 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.
此外,类文件不是机器可读的/二进制的,就像编译的 c 文件一样。用你的文本编辑器打开一个类文件,你会看到它实际上是一个文本指令列表。Java 虚拟机查看这些类文件并解释它们,执行结果指令。

