Dynamic Loading of Classes.

A

adil.fulara

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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.

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
 
A

adil.fulara

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

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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 ?

No.

You do not need to do anything with classloaders in
your code just a simple Class.forName !

Arne
 
A

adil.fulara

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
 
L

Lew

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.

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

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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
 
G

Guest

Lew said:
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
 
A

adil.fulara

Reflection is very widely used not just for JDBC. But often it happens
below the user application. Example: Spring.


Often the two are combined.


Reflection is not that hard to learn, but reinventing RMI for
sending class definition and objects over the wire is a biggie.

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
 
K

Karl Uppiano

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

Karl Uppiano

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.

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

Lew

Karl said:
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.

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

adil.fulara

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
 
L

Lew

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>
 
M

Matt Atterbury

Lew said:
Your future lack of top-posting will be thanks enough.


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.

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

blmblm

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.

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

adil.fulara

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.

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

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.

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

blmblm

[ snip ]

[ snip ]

[ 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?
 
R

RedGrittyBrick

<snip debate about "doubt" vs "question">

somebody said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top