Author Topic: Netbeans help  (Read 3133 times)

0 Members and 1 Guest are viewing this topic.

Offline Rick LawTopic starter

  • Super Contributor
  • ***
  • Posts: 3441
  • Country: us
Netbeans help
« on: July 24, 2015, 01:21:02 am »
Ok, I am trying to be a hero (helping my kid) and I am now looking like an idiot... 

I am helping her move a working command prompt Java program into netbeans IDE so she can use the debugger to do the more complex things in the assignment.  I am running into a problem importing a 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 called Input.class).  Under command prompt, I write a program with Input.something() and it works.  It compiles without an import command 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 given Input.class file 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 .class into .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 the Input.jar with the given Input.class file.

Inside netbean:
- I created a "Hello World" project, and the System.out.println("Hello world") works.
- With this working project, I add the Input.jar to the library
- I go into the project's Libraries folder and right click to Add JAR/Folder
- I selected the Input.jar and added it.
- In netbean Project window, I can see the Libraries folder now has Input.jar
- In netbean Navigator window, I can navigate to Libraries folder and see it.
   Libraries
           Input.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 the import command to import this JAR package into my "Hello World" project.
  "import    Input.*;" doesn't work.  I don't have a package call Input.  (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 putting Input.jar in 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?

Thanks
Rick

 

Offline Deathwish

  • Supporter
  • ****
  • Posts: 1424
  • Country: wales
Re: Netbeans help
« Reply #1 on: July 24, 2015, 01:25:05 am »
If you cant get an answer here have you tried the Devshed forum ?.

Electrons are typically male, always looking for any hole to get into.
trying to strangle someone who talks out of their rectal cavity will fail, they can still breath.
God hates North Wales, he has put my home address on the blacklist of all couriers with instructions to divert all parcels.
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Netbeans help
« Reply #2 on: July 24, 2015, 12:39:01 pm »
Are you using the correct namespace?

If you post a link to your project we can have a look for you.
 

Offline Rick LawTopic starter

  • Super Contributor
  • ***
  • Posts: 3441
  • Country: us
Re: Netbeans help
« Reply #3 on: July 24, 2015, 03:59:52 pm »
Are you using the correct namespace?

If you post a link to your project we can have a look for you.

Hmmm...  I am not sure.  I will have to research on namespace.

Trouble with learning on your own.  I may be missing some fundamentals that I didn't know I am missing.
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Netbeans help
« Reply #4 on: July 24, 2015, 04:22:48 pm »
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.
 

Offline codeboy2k

  • Super Contributor
  • ***
  • Posts: 1836
  • Country: ca
Re: Netbeans help
« Reply #5 on: July 25, 2015, 12:13:28 am »
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.

Wilksey 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.
 

Offline Rick LawTopic starter

  • Super Contributor
  • ***
  • Posts: 3441
  • Country: us
Re: Netbeans help
« Reply #6 on: July 27, 2015, 10:20:21 pm »
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
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf