Netbeans help - Page 1
Forum section: electronics / beginners

author: Rick Law
author_rank: Super Contributor
timestamp: July 24, 2015, 01:21:02 am
content: Ok, I am trying to be a hero (helping my kid) and I am now looking like an idiot...I am helping her move aworking command prompt Java programinto netbeans IDE so she can use the debugger to do the more complex things in the assignment. I am running into a problem importinga class file she is given that she must use.Within netbeans, I've imported things I wrote before. This is the first time I try to use a class I didn't write but just given to me (in a file calledInput.class). Under command prompt, I write a program with Input.something() and it works. It compiles without animportcommand in the code, I just put the Input.class in the same directory and it compiles and runs OK calling the methods in Input.class.So, with a working simple test program, I am ready and moving onto netbeans. But I am having problem incorporating this givenInput.classfile into a netbean project.The lines of test codes to test it is simple:...int thisInteger = Input.getInt("Enter the number");System.out.println(thisInteger);...I found no way to directly use a .class file inside netbeans. The libraries and other tools seem to demand a .jar file. So I begun test with first convert the.classinto.jar, then add JAR into the library and test the code with a simple "Hello World" program.In command prompt:- I use "jar cvf Input.jar Input.class" That created theInput.jarwith the givenInput.classfile.Inside netbean:- I created a "Hello World" project, and the System.out.println("Hello world") works.- With this working project, I add theInput.jarto the library- I go into the project'sLibrariesfolder and right click toAdd JAR/Folder- I selected theInput.jarand added it.- In netbeanProject window, I can see theLibrariesfolder now hasInput.jar- In netbeanNavigator window, I can navigate toLibrariesfolder and see it.LibrariesInput.jar<default package>Input.class<META-INF>MANIFEST.MF- So, it looks like Input.jar is added and it has Input.class which has Input.getInt() method.- BUT... adding a line to use Input.getInt(), Input.getInt() is undefined.- So, simple I thought, I need to add theimportcommand to import this JAR package into my "Hello World" project."import Input.*;" doesn't work. I don't have a package callInput. (but Input.jar is in the library).I am missing something. Import expects a package name, but this .class file is given to me so I don't know what package if any this Input class is under.With other JARs, I put it into the main library and they work. So, I directly puttingInput.jarin the ...jre\lib\ext\ folder to try but that doesn't work either. With the other JARs I tried before, I know the package name and I know how the how "import packageName.*" should look. This one, I just can't get the imported class methods to be recognized.What am I doing wrong here? Is there another way to import a .class directly?ThanksRick

author: Deathwish
author_rank: Unknown
timestamp: July 24, 2015, 01:25:05 am
content: If you cant get an answer here have you tried the Devshed forum ?.

author: Wilksey
author_rank: Super Contributor
timestamp: July 24, 2015, 12:39:01 pm
content: Are you using the correct namespace?If you post a link to your project we can have a look for you.

author: Rick Law
author_rank: Super Contributor
timestamp: July 24, 2015, 03:59:52 pm
content: Quote from: Wilksey on July 24, 2015, 12:39:01 pmHmmm... I am not sure. I will have to research onnamespace.Trouble with learning on your own. I may be missing some fundamentals that I didn't know I am missing.

author: Wilksey
author_rank: Super Contributor
timestamp: July 24, 2015, 04:22:48 pm
content: If you import your class files from elsewhere, the package name might be different, which is why I said do you have the correct namespace.

author: codeboy2k
author_rank: Super Contributor
timestamp: July 25, 2015, 12:13:28 am
content: Quote from: Wilksey on July 24, 2015, 04:22:48 pmWilksey is on the right track here. It's a namespace problem. The biggest being that your Input.class code that was supplied to the students is doing something that is considered bad practice in Java code, and that is -> using the default package namespace. You cannot access the default package namespace from inside another package. The Input.class should be inside some x.y.x package namespace so you can import it properly. The author of the package got lazy (or didn't want to confuse the students with package issues when using the school supplied Input.class)I don't know netbeans well enough to suggest an answer, although I understand the problem here.You'll likely need to just put the .jar file somewhere in your CLASSPATH, (or the current directory and make sure that '.' is in the CLASSPATH) or extract the .class file and put it singularily as part of the build line. Those are some suggestions that might work.

author: Rick Law
author_rank: Super Contributor
timestamp: July 27, 2015, 10:20:21 pm
content: Yeah, codeboy2k and wilksey, I think you guys are right. Thanks.I was able to recreate the same thing by writing a class in netbeans and then trying to call it from DOS prompt and from within another netbeans project. The trial with the to-be-use-class with specific package name went ok. The trial of to-be-use-class in default package didn't - it exhibits the exact behavior like the class I was trying to help my daughter with.She got her stuff done without needing netbeans' debugger. (it was a dos-prompt based exercise). I will experiment with that some more. I am curious and like to see if I can get around that.Thanks, guys!Rick