?
Solved

Netbeans and class files

Posted on 2006-04-24
32
Medium Priority
?
708 Views
Last Modified: 2013-11-23
I am trying to use a Java API for an off the shelf piece of vendor software.

The files included in this JAVA API are a bunch of compiled  *.Class  files.  I am having a hard time figuring out how to import these into my project.  For other API's I just import their Jar files and I'm good to go.  Not sure how to import the class files in so I can use them.  I think you have to put them in a package....I don't know.  Can't seem to figure it out.

How can I include and use these compiled *.Class files??

Thanks in advance experts

gusseology
0
Comment
Question by:gusseology
  • 11
  • 7
  • 7
  • +3
32 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16529485
you will have to make the package according to how they've been compiled
0
 

Author Comment

by:gusseology
ID: 16529627
Hmmm.  I'm still kinda lost.

I mean, I have 10 or so .Class files.  I want access to them all.  It makes sense that they need to be in a package.  So how do I create the package accoding to how they've been compiled?  I don't know how they were compiled or anything about them other than the methods available to me.

I tried selecting them all and creating a zip and renaming to a jar, then importing that.  But it didn't work.  It put them in <default package> and I wasn't able to access them.

Sorry for my lack of knowledge.  I'm a newbie to Netbeans and Java.  Could you provide some more detailed guidance??

vr,

gusseology
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16529935
I don't use Netbeans, but you could try 'adding them to the project' or importing them or some such
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:gusseology
ID: 16529982
yea, that's right where I'm getting tripped up.  You'd think it would be a simple task to add/import/etc. .class files to the project.  But it's not quite working out when I attempt it.

gusseology
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16530066
If you open the class file in a binary editor, the first readable characters you see will show you the package
0
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
ID: 16530440
The class files should already be in the required directory structure (matching the package hierarchy) in the api distribution.
You need to add the base *directory* of the class hierarchy to your project.
0
 
LVL 23

Accepted Solution

by:
Siva Prasanna Kumar earned 600 total points
ID: 16530889
I think there are two ways to go about this in NetBeans 1st one Make sure the Package structure of the class files is correct (hierarchy) and then do this

Right click on Source Packages folder of your Project create the same structure of folders and put those class  files in it.

else

Right click on Libraries folder of you project and select the Add Folder/Jar option and add the folder(top folder if in package) conatining the classes.

the only other option left which will surely work is to create a jar file and add it in the liraries folder as per the 2nd step metioned above.

Thank You.
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 150 total points
ID: 16531876
Try to JAR the API's class-files from their root-directory and simply add the JAR to the project's build-path.

http://java.sun.com/docs/books/tutorial/deployment/jar
0
 
LVL 9

Expert Comment

by:riaancornelius
ID: 16531972
>> Try to JAR the API's class-files from their root-directory and simply add the JAR to the project's build-path.
>> http://java.sun.com/docs/books/tutorial/deployment/jar

That's correct. Then when you have a jar file, you can either put that jar somewhere in your classpath ( like: java/jdk/jre/lib/ext ) But I don't like this method. What I'd rather do is either mount the jar in netbeans (if it's version 3.5 or 3.6), or otherwise include it in your projects libraries for later versions...
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16531985
>> But I don't like this method.

Its reusable for all IDEs ;-)
0
 
LVL 9

Expert Comment

by:riaancornelius
ID: 16532033
Yes, But it can be quite difficult to debug if something goes wrong, and it's also shared by all java apps, so if somebody else overwrites a library with an earlier version, your app stops working....

If it's only for dev, I'd say sure, go for it. Just make sure that if you distribute it that you have a better way of doing it :)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16532048
>> But it can be quite difficult to debug if something goes wrong,

Well, then that can be said about all JAR files ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16532906
The issue really doesn't have much to do with jars. Once it's in the correct place (see how to find that place in my last comment) your can either incorporate it into a jar or not depending on your requirements
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16533621
Yes, it was just another option to make it more redistributable.
0
 

Author Comment

by:gusseology
ID: 16533863
Thanks for all the comments!  Sorry I didn't get back sooner.  

I actually tried (or at least think I tried) the options you outlined.  

I tried creating a jar by two different methods.  One was zipping the files together and renaming it to a jar.  THe other way I tried it was by using the jar.exe program and creating one.

So anyway, now I have a jar file created form the class files and then I attempt to import it in the same way I import any other JAVA API jar file (by right clicking on library, Add Jar..etc etc.).  That's where I get the problem.  With the other Java API's I have, when I add their Jar files, they get imported and are located in a package structure.  And referencing them is easy, i say import bleah.bleah.bleah.*

However, when I import the Jar I'm having problems with, I get a package called <default package> and under it are the class files.  And I can't seem to reference the package named <default package>.

gusseology
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16533885
Forget jars for the moment - they're not relevant. Did you follow the methodology i suggested (i suspect not)?
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16533944
Just try my suggestion and get back.

Thank You.
0
 

Author Comment

by:gusseology
ID: 16533997
Oh, opening the binary file and finding the package location.  Sorry, haven't tried that.  I'll try it now.

The reason I shyed away from it is because it seems overkill.  There has got to be a way to simply import class files into a project in Netbeans and let your app use them.  I mean....they're class files!  You have to be able to reference them!  Right?  

It seems my original question should be readily available and easy to learn, but for some reason I can't simply import and use class files.

But thanks for the suggestion CEHJ!  Too complex or not, I'll give it a shot.  But I still think there has to be a more elegant and simple way to import and use class files

gusseology
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16534002
>>The class files should already be in the required directory structure (matching the package hierarchy)

>>Make sure the Package structure of the class files is correct (hierarchy)

You can't determine that without internal inspection once the class files have become disconnected from their original context, as seems to be the case here.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16534051
>>There has got to be a way to simply import class files into a project

Well that's reasonable, but i haven't seen *much* helpfulness from IDEs with arbitrary imports
0
 
LVL 92

Expert Comment

by:objects
ID: 16538875
I doubt they are in the default package, and if they are then you can't reference them anyways.
Did you check how they were stored in the api distribution, that should give you the package hierarchy and allow you to add them all to your project by adding the top directory.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16538932
Are any 3rd party APIs distributed not as JARs/ Zips or something of the like? If you can find out the original distributable you would have something handy like that (check with your colleagues or see if it is a free download on the Net?).
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16538954
There's no need for all these enquiries - just look in the class file ;-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16539017
That would be the faster way to make it work but I would prefer having it JARed as the better way to make it work in the long run.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16539054
Yes but jarring it won't affect the problem - the package hierarchy will have to be discovered anyway
0
 

Author Comment

by:gusseology
ID: 16539104
I got it guys...thanks for the comments, I'll distribute points later.  I only have a minute, so I wanted to post the solution now and I'll figure out how to distirbute the points fairly later.

Here is what you do.  Go to the project.  Choose the sources section.  Right click and choose new and then choose java package.  Once that package is created (we'll call it packagename), it will be empty.  Go to that packagename's directory in widows explorer and drop all the class files off in that directory.  Build the project.

Then go to the libraries section, right click and choose add jar/folder.  add the folder containing those classes.

In the main.java you can put
import packagename.*;

And then you know have access to all the methods in all those .class files.  Your'e good to go at this point.

I will detail the steps tomorrow when I have more time, but that's pretty much it in a nutshell.

thanks for everyone's suggestions!

gusseology
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16539326
>> the package hierarchy will have to be discovered anyway

Of course, JARring will have to be done from the root directory once it is known.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 600 total points
ID: 16541538
>>Here is what you do. ..

If that's a success, it suggests that the classes were in the default package. Dropping them into any of *your* packages and then rebuilding should work too.
0
 

Author Comment

by:gusseology
ID: 16577980
sorry for the delay in responding.

It was a success, I simply had to create a package, then drop the class files into that package (via windows explorer) and rebuild.  I was good to go.

Thanks for all the responses, I will distribute points.

gusseology
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16578011
Is that according to My first suggestion??
0
 

Author Comment

by:gusseology
ID: 16578556
Looking back yea, it looks like it is much like your first suggestion.  But at the time, your first suggestion didn't make much sense to me.  Guess it's one of those things I had to have detailed directions or they didn't make sense.

thanks for all your help

gusseology
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16578709
:-)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question