How to use a class file?

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

I understand that the class file is machine-readable byte code, but I don't care what happens under the hood. I would just like to import it into my current project and get to know it so that I can use the class.

In addition, the class file is embedded in the JAR file. I imported the JAR file into the folder / tab of my libraries in the projects window, but I don’t know how to get my project to recognize the class. It says “cannot find character” whenever I try to instantiate an object.

+6
java class netbeans
source share
4 answers

You must import by calling the name of the source package. i.e. import hello.Car; , In your case, you are trying to call import by the name of the JAR folder, which leads to the error "cannot find the character".

Let me give you an example for a better understanding.

  • Consider this simple Vehicle application that has a Car class and a Test Car alt text

  • Convert it to jar and add to another project named ImportVehicleJar

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

    • To create an instance of the Car class in the Main.Java file, add the following, as shown in the figure. alt text

    Hope this helps !!

+10
source share

In Netbeans (version 5.5.1), you can add a jar file to a project by right-clicking on the project name and selecting "Properties", then "Libraries" (from the category), as well as the "Add JAR / Folder" button, which you can use to add it to the compilation path and / or runtime. Adding it to compile-time libraries is good enough, because it will be automatically added at run time through the "Classpath for Compiling Sources" entry.

+2
source share

You either need to specify the full package path to the class. eg

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

or use the import statement

 import com.foobar.acme.Clobula ... . Clobula myBigClobula = new Clobula(); 
+1
source share

In addition, class files are not machine readable / binary in the sense that compiled files are c. Open the class file with a text editor and you will see that this is actually a list of text instructions. The Java virtual machine scans these class files and interprets them, following the instructions received.

+1
source share

All Articles