views:

307

answers:

5

Hi, 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.

+2  A: 

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.

JuanZe
+1  A: 

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();
GregS
+1  A: 

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.

darren
I think Alex is meaning ".class" files, which are the files the JVM loads. These files are binary, they are not machine-code file like C files, but they are **not** text files. ".java" are text files which are compiled into bytecode to produce the ".class" file. The classfile definition: http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html
Carlos Heuberger
.java files are the sourcecode. .classfiles are very much readable. Open one in a text editor.
darren
I do not know what you mean as readable, but any classfile I opened (the last 10 years) is NOT a text file. Reading the classfile definition just confirms that: they start with magic code (0xCA 0xFE 0xBA 0xBE), followed by 3 2-byte integers for minor, major and constant-pool-count. Please send me an exampe of a (valid) classfile that is "readable". Here's what I see when opening one in a text editor: http://img254.imageshack.us/img254/6135/helloi.jpg and here the classfile: http://simu.wikidot.com/local--files/java:java/Hello.class
Carlos Heuberger
From what you've posted, I can see that you have a simple program with a single System.out.println statement. The methods / instructions are in text (hence readable). Conversely, try looking at a compiled C file in a text editor, you do not get the same textual instructions.
darren
just because **some** text is readable it is not a text file. You can not see (as text) if there's an `if`, other flow control commands or arithmetic in the code. Executable C also have a TEXT segment where you can read which strings are used, same true for imported library functions (as I remember).
Carlos Heuberger
ahh i see what you mean. You are correct. I thought that all of instructions were in text form in a class file, similar to what assembly would look like. After some simple test programs, the instructions are in binary op codes (java bytecode). Thanks for the clarification.
darren
+6  A: 
YetAnotherCoder
A: 

Thanks all! With your answers combined I was able to get the class to be recognized.

ShrimpCrackers