Serialize a class (not an object)

C

cyberco

I want to send a class (compiled java file, not an object) over RMI so
that the receiving party can instantiate objects with that class file.
Using an RMI codebase is sub-optimal since the type of class is
dynamically generated and will thus change very often. Is there a way
to serialize the class file, send it to the receiver, have him add it
to his classpath and use it to instantiate objects?
 
O

Oliver Wong

cyberco said:
I want to send a class (compiled java file, not an object) over RMI so
that the receiving party can instantiate objects with that class file.
Using an RMI codebase is sub-optimal since the type of class is
dynamically generated and will thus change very often. Is there a way
to serialize the class file, send it to the receiver, have him add it
to his classpath and use it to instantiate objects?

Serialization is taking conceptual data, and turning it into a sequence
of bytes. Assuming you actually HAVE the class *FILE* somewhere, the file
itself is nothing more than a sequence of bytes. So just send the file.

- Oliver
 
J

James McGill

So just send the file.

So on the receiving end, what kind of hoops do you have to jump through
to get a classloader to load such a file? Just curious.

James
 
O

Oliver Wong

James McGill said:
So on the receiving end, what kind of hoops do you have to jump through
to get a classloader to load such a file? Just curious.

From http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html:

<quote>
A class loader is an object that is responsible for loading classes. The
class ClassLoader is an abstract class. Given the binary name of a class, a
class loader should attempt to locate or generate data that constitutes a
definition for the class. A typical strategy is to transform the name into a
file name and then read a "class file" of that name from a file system.
[...]
Normally, the Java virtual machine loads classes from the local file system
in a platform-dependent manner. For example, on UNIX systems, the virtual
machine loads classes from the directory defined by the CLASSPATH
environment variable.

However, some classes may not originate from a file; they may originate from
other sources, such as the network, or they could be constructed by an
application. The method defineClass converts an array of bytes into an
instance of class Class. Instances of this newly defined class can be
created using Class.newInstance.
</quote>

- Oliver
 
T

Tony Morris

cyberco said:
I want to send a class (compiled java file, not an object) over RMI so
that the receiving party can instantiate objects with that class file.
Using an RMI codebase is sub-optimal since the type of class is
dynamically generated and will thus change very often. Is there a way
to serialize the class file, send it to the receiver, have him add it
to his classpath and use it to instantiate objects?

RMI already does that transparently. You just have to ensure that your
SecurityManager permits the loading of the class over the wire.
 
G

gjlubbertsen

I want to do the same as cyberco. I generate some source code
(dynamicly) and i want to compile it when a client asks for the code.
At the moment i'm looking at www.janino.net's inline compiler. Is it
good or is there a better one? I'd rather not use the filesystem to
store the source and then run javac.

With Janino i can compile and instantiate classes so the compiling part
is working.

Tony Morris schreef:
RMI already does that transparently. You just have to ensure that your
SecurityManager permits the loading of the class over the wire.

Tony. Could you describe how i can send this (in memory) class to the
client so it can instantiate and run it? Do you have some useful URL's?

tnx
GJ
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top