Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 418
ShrimpCrackers Avatar asked Dec 31 '09 01:12

ShrimpCrackers


People also ask

How to create a class file in Java?

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.

Why do Java programs have different class files for each class?

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.

How do you use multiple classes in Java?

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.

How many public classes can a Java source file contain?

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.


2 Answers

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 alt text

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

    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 alt text

    Hope this helps !!

like image 62
Srinivas M.V. Avatar answered Oct 11 '22 22:10

Srinivas M.V.


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.

like image 22
JuanZe Avatar answered Oct 11 '22 21:10

JuanZe