Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Serialize a class (not an object)

Thread view: 
cyberco - 04 May 2006 16:12 GMT
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?
Oliver Wong - 04 May 2006 16:49 GMT
>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
James McGill - 04 May 2006 17:34 GMT
> 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
Oliver Wong - 04 May 2006 18:08 GMT
>> 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.

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
Tony Morris - 05 May 2006 00:12 GMT
>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.

Signature

Tony Morris
http://tmorris.net/

gjlubbertsen@gmail.com - 05 May 2006 17:32 GMT
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


Free Magazines

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

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.