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.
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.
Remember java compiler or javac command is used to create a class file from the java source file, and the java command is used to run a Java program stored in a class file. Since the class file contains bytecode in hex format and the class file format is well-documented, anyone can temper with the class file and break Java security grantees.
If a Java program has more than one class, in such cases after compiling the source file, we get the same number of .class files as the number of classes a Java program has. So, the compiler creates class files depending on the number of classes declared in that Java source file. //Declaring HelloWorld class.
Using Multiple Classes. You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)). Remember that the name of the java file should match the class name.
One Java source file can only contain one public class, and its name must match with the name of the file like HelloWorld.java file can contain a public class whose name should be HelloWorld as shown below : A class file in Java has a .class extension.
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
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 !!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With