I was trying to play around with Netbeans 7.0.1 yesterday with the imports and packages. I wanted to import a class that I had created earlier into a new project. It took me a lot of time to actually do this simple task because I was ignorant of the fact that I did not have much Netbeans experience. At first I tried to just create a jar file like normal and have the class I wanted in a default package. However, this turned out futile and useless as I was unable to import anything.
For example,
my java class is called classA and it wasnt' in any default package. When I created the jar I just went to the directory that classA was in and created the jar form there, say MyJar.jar. I then proceeded to import MyJar.jar into the netbeans library for the particular package. When I imported classA using import classA, it would not allow me to use it as it was not found.
After reading on several text, I decided to create a jar using directories and subdirectories instead of creating the jar on the class firsthand. I put classA into directories
org/test/
and changed the java file to allow and inserted the code " package org.test" on top
I then went into the directory above org, and proceeded to create a jar by using the following command:
jar -cvf MyJar.jar org
this would package everything neatly and when I imported into Netbenas the jar MyJar.jar, I was able to use classA by using the code
"import org.test.classA"
This works at the moment because I was able to change the source code of classA by putting it into a proper package. However, what would happen if a class file is all I am given and I do not have the source code? There should be some way in which I am able to add a class into the library in Netbeans so I can use it. I will edit this post more once I have found this method
No comments:
Post a Comment