Java Forum / General / April 2007
Dynamic Loading of Classes.
adil.fulara@gmail.com - 07 Apr 2007 02:22 GMT Hey group,
I am trying to load a .class file which is in the same folder as the main class i invoke. Somehow it is not able to find it. I am basically trying to load a file dynamicaly, do some work and send the same class file to a server (remote or local) to do some other work using the same class file.
I did look up at the reflection API of java and ClassLoader but at the moment i seem to be stuck at the beginning with regards to loading the desired file leave alone sending it.
Any pointers.
Thank You,
A
Arne Vajhøj - 07 Apr 2007 02:28 GMT > I am trying to load a .class file which is in the same folder as the > main class i invoke. Somehow it is not able to find it. I am basically [quoted text clipped - 5 lines] > moment i seem to be stuck at the beginning with regards to loading the > desired file leave alone sending it. Loading it locally if it is in classpath is just a matter of calling Class.forName("mypackage.MyClass").
Sending it to a server ? Do you want to send an object of that class ? Do you want to send the class definition ? Do you want to send both ? I think you should consider RMI, because it provides some of this functionality.
Arne
adil.fulara@gmail.com - 07 Apr 2007 02:34 GMT Well i will tell you what i did. i have two classes Main and mysteryClass. Both are in the same folder. Main class extends ClassLoader. in the loadClass(), i tried giving the name of the mysteryClass but somehow i got
java.lang.NoClassDefFoundError exception.
Is it the right way of doing it ?
A.
> adil.ful...@gmail.com wrote: > > I am trying to load a .class file which is in the same folder as the [quoted text clipped - 16 lines] > > Arne Arne Vajhøj - 07 Apr 2007 03:13 GMT > Well i will tell you what i did. i have two classes Main and > mysteryClass. Both are in the same folder. Main class extends [quoted text clipped - 4 lines] > > Is it the right way of doing it ? No.
You do not need to do anything with classloaders in your code just a simple Class.forName !
Arne
adil.fulara@gmail.com - 07 Apr 2007 04:16 GMT Hi,
Thank you so much. I am able to load the class.
Now can you tell me how do i unload the a class since i need to send the class file on the server side.
Is there a way to do that.
A
> adil.ful...@gmail.com wrote: > > Well i will tell you what i did. i have two classes Main and [quoted text clipped - 12 lines] > > Arne Lew - 07 Apr 2007 13:12 GMT > Hi, > [quoted text clipped - 4 lines] > > Is there a way to do that. First, please stop top-posting.
You do not send classes over the wire. You could send bytecode over the wire, but that involves tricks like URLClassLoader. None of these tricks involve "unloading" a class - the term has no real operational meaning.
The real question is why you even need reflection in the first place. In virtually all situations in Java where one is attempting a reflective solution one is better served using an object-oriented approach, and one gains huge benefits by using the more static approach. Yes, there are times when Class.forName() is useful, such as to load a JDBC Driver, but this is a limited use of reflection. More complex uses are usually needless.
Most of what people naively attempt to solve with reflection is better solved with polymorphism.
If you do need reflection, you probably don't need a ClassLoader, as Arne pointed out, Even if you did, you likely don't need to extend ClassLoader, or even use a custom ClassLoader at all. The standard ClassLoaders should work just fine for you.
Furthermore, reflection is a fairly advanced topic. You should be more comfortable with Java and principles like "prefer composition to inheritance" and "prefer inheritance to reflection" before you venture into such deep waters.
 Signature Lew
Arne Vajhøj - 07 Apr 2007 17:11 GMT > In virtually all situations in Java where one is attempting a reflective > solution one is better served using an object-oriented approach, and one > gains huge benefits by using the more static approach. Yes, there are > times when Class.forName() is useful, such as to load a JDBC Driver, but > this is a limited use of reflection. More complex uses are usually > needless. Reflection is very widely used not just for JDBC. But often it happens below the user application. Example: Spring.
> Most of what people naively attempt to solve with reflection is better > solved with polymorphism. Often the two are combined.
> Furthermore, reflection is a fairly advanced topic. You should be more > comfortable with Java and principles like "prefer composition to > inheritance" and "prefer inheritance to reflection" before you venture > into such deep waters. Reflection is not that hard to learn, but reinventing RMI for sending class definition and objects over the wire is a biggie.
Arne
adil.fulara@gmail.com - 07 Apr 2007 20:59 GMT > > In virtually all situations in Java where one is attempting a reflective > > solution one is better served using an object-oriented approach, and one [quoted text clipped - 20 lines] > > Arne Well , i am not expert like you... So if you think why i need to use all this is just becoz i m taking a course and the prof gave me guidelines to do it....
And yea i cant use RMI. I have to use Middleware which is developed by the prof. Just say it is an event based middleware. What i need to do is, my client has a class loaded which needs to be dispatched to the server. For this the guidelines specified me to use Reflection and Classloaders.
So although i would want to use RMI, i cant.
And since i m not an expert java developer, its being going tough doing all this....
At the moment i am just trying to serialise the class i want to send to the server but i just dont kno how its done... Al examples i see either output the serialised object to a file or put it on a socket. Since i gotta use an event based middleware of which i dont kno the details, i dont kno how i am gonna be saving this serialised object and then sending the whole thing throug an event. i have the connection setup, so displatching shouldnt be a problem. its just that i need to get this serialised object into a form which i cant put into an event.
Thanks.
A
Karl Uppiano - 07 Apr 2007 23:56 GMT I haven't tried this personally, but in theory, you should be able to use an ObjectOutputStream.writeObject to write to a ByteArrayOutputStream instead of a file or socket, and put the byte array into an event.
At the other end, you can use a ByteArrayInputStream and ObjectInputStream.readObject.
All of the non-transient, serializable member data should go across the wire.
Karl Uppiano - 08 Apr 2007 00:00 GMT > The real question is why you even need reflection in the first place. In > virtually all situations in Java where one is attempting a reflective [quoted text clipped - 3 lines] > this is a limited use of reflection. More complex uses are usually > needless. In situations where someone is building a "pluggable" framework using interfaces, and the implementor is unknown at design time, reflection, e.g., Class.forName, is the only way I know to accomplish this.
Lew - 08 Apr 2007 06:17 GMT >> The real question is why you even need reflection in the first place. In >> virtually all situations in Java where one is attempting a reflective [quoted text clipped - 7 lines] > interfaces, and the implementor is unknown at design time, reflection, e.g., > Class.forName, is the only way I know to accomplish this. Which is roughly what the OP said, thus answering the real question.
They also said that this was an academic exercise wherein the use of reflection and class loaders was required.
It remains true that for the largest category of projects reflection is used lightly, generally not much past using Class.forName() or using Class<T> as a runtime type marker. The full marvelous power of reflection is inarguably useful in those scenarios where it is needed.
 Signature Lew
adil.fulara@gmail.com - 10 Apr 2007 18:59 GMT Hi,
Thank you all so much for the help. I was able to transfer a class from client to server as suggested by you all....
I have couple of more related doubts. The class i sent had a transient object of a serializable class X and utilizes some classes from a jar file. So in order for me to use the transferred class i need to send these additional class and jar on the other side.
Any pointers on how to send these files over. I havent created an object of class X, so would i need to create an instance of X and then follow the same procedue described above to send it over to the other side. How would i apply the sending process to the JAR?
Also how do i invoke the main class object which used the Class X and jar ? i mean when i transfer it on the other side, how will it know where to find the class X and JAR file ?
Kindly advise....
Thanks a lot guys.
A
> >> The real question is why you even need reflection in the first place. In > >> virtually all situations in Java where one is attempting a reflective [quoted text clipped - 20 lines] > -- > Lew Lew - 11 Apr 2007 00:36 GMT > Hi, > > Thank you all so much for the help. I Your future lack of top-posting will be thanks enough.
> I have couple of more related doubts. No, you have a couple more related questions. A "doubt" is a disbelief, a suspicion, an element of incredulity, a lack of confidence or trust.
A lot of people have been posting that they have "doubts", then posting questions but no doubts.
I suspect that someone's translation software is not giving the right results.
I doubt that anyone intentionally misused the word.
<http://en.wiktionary.org/wiki/doubt> <http://www.m-w.com/dictionary/doubt>
 Signature Lew
Matt Atterbury - 11 Apr 2007 04:02 GMT > > Hi, > > Thank you all so much for the help. I [quoted text clipped - 13 lines] > > I doubt that anyone intentionally misused the word. In my limited experience, some people whose native language is an Indian (subcontinent) one tend to use "doubt" when I would use "question". I have assumed that it was because of some mismatch between their native word(s) and the English words.
m.
blmblm@myrealbox.com - 11 Apr 2007 13:12 GMT > > > Hi, > > > Thank you all so much for the help. I [quoted text clipped - 18 lines] > assumed that it was because of some mismatch between their native word(s) > and the English words. I posted an attempted correction along the same lines as Lew's in comp.lang.fortran not long ago, and one person responded as you did (that this seemed to be standard usage in the Indian version of English). But he added that these people seemed to have learned English fairly young, so in a sense it's their native language too. Here's the post:
http://groups.google.com/group/comp.lang.fortran/msg/863bc908006bfa26?hl=en&
In the c.l.f thread, the difference in usage made for a subject line that was potentially misleading -- "starting to doubt Fortran" where "starting to consider learning Fortran" seemed more apt to me. The funny part is that at some point the thread drifted in a direction more like what this US English speaker would have expected based on the subject line.
The old phrase "two countries [ US and UK ] divided by a common language" may need to be expanded a bit.
 Signature Decline To State (But the e-mail address in the header is real.)
adil.fulara@gmail.com - 11 Apr 2007 21:25 GMT On Apr 11, 5:12 am, blm...@myrealbox.com <blm...@myrealbox.com> wrote:
> In article <877isjaa02....@virgo.contecint.com.au>, > [quoted text clipped - 44 lines] > Decline To State > (But the e-mail address in the header is real.) Alright. I accept that i may have used words that might have sounded confusing, but hey.... I got my point forward right...... As some other members pointed out, its in our system.
It would be great if i got some pointers on the real problem. Lol!!!
A.
blmblm@myrealbox.com - 12 Apr 2007 05:58 GMT > On Apr 11, 5:12 am, blm...@myrealbox.com <blm...@myrealbox.com> wrote: > > In article <877isjaa02....@virgo.contecint.com.au>, > > > > > > adil.ful...@gmail.com wrote: [ snip ]
> > > > > I have couple of more related doubts. > > [quoted text clipped - 4 lines] > > > > A lot of people have been posting that they have "doubts", then > > > > posting questions but no doubts. [ snip ]
> > > > I suspect that someone's translation software is not giving the > right results. [quoted text clipped - 14 lines] > > > > http://groups.google.com/group/comp.lang.fortran/msg/863bc908006bfa26... [ snip ]
> Alright. I accept that i may have used words that might have sounded > confusing, but hey.... I got my point forward right...... As some > other members pointed out, its in our system. > > It would be great if i got some pointers on the real problem. Lol!!! Agreed, but unfortunately that won't happen in this post (no time to review the original questions).
I'm just chiming in again to say that if you feel like you're being picked on for something trivial, well, that wasn't my intent, and I at least am starting to believe that your usage really is standard and correct for your version of English rather than just being a mistake. It's just that it confuses some of us US English types, and maybe it's useful for you to know that?
 Signature Decline To State (But the e-mail address in the header is real.)
RedGrittyBrick - 12 Apr 2007 10:34 GMT somebody wrote:
>> I have couple of more related doubts. <snip debate about "doubt" vs "question">
somebody else wrote:
> I'm just chiming in again to say that if you feel like you're being > picked on for something trivial, well, that wasn't my intent, and I > at least am starting to believe that your usage really is standard > and correct for your version of English rather than just being a > mistake. It's just that it confuses some of us US English types, and > maybe it's useful for you to know that? I think it's not just US English types who find it jarring.
I have to admit that I've been sorely tempted many times to comment on the use of "doubt" where I'd have used "question". So far I've resisted. However, as a UK English speaker I do find it irritating. To me, if you have a doubt then you should ask questions to resolve that doubt.
I have an ever so slight feeling of uncertainty: Maybe in some cultures it is rude to ask a question and people resort to politely expressing doubts so that the listener doesn't feel they are expected to provide an answer?
I have another ever so slight feeling of uncertainty: Will any members of this newsgroup be offended if I am so bold and brazen as to actually ask questions?
Arne Vajhøj - 07 Apr 2007 17:06 GMT > Now can you tell me how do i unload the a class since i need to send > the class file on the server side. > > Is there a way to do that. You do not need to unload the class to do that.
If you need to unload a class you need to load it with a special class loader and drop that.
Regarding sending to the server, then try look at RMI.
Arne
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|