Solved

Dynamically load a class

Posted on 2007-03-31
3
191 Views
Last Modified: 2013-11-23
I kind of at a wall with this problem and need some help. If I could award more points I would. My issue is this:  I have a client-server system which is designed for others to extend. I have a a base interface, let's call it "MyRunnable." This interface has one method:  void run().      

I need to be able to allow someone to build their own client application to talk to my server (using Web Services). I need someone to, using my class library, construct a class that implements MyRunnable.and allow the server to invoke the run() method on an instance of that object.

Essentially, I believe I need to allow the ability have an instance of a MyRunnable object be serialized, sent to the Web Service, then the server deserialize it and invoke the method. But obviously, I need the class definition to be available to server and all of that.

I've looked at things like the ClassLoader and all of that but I need some guidance. I know this is non-trivial but hopefully someone can point me in the right direction. There is a project called JPPF (a grid computing project) which does something like this but it's implementation is far to complex/robust than what I need. So any help is appreciated.

Again, put simply: I need my library to allow for an object in one application X, that implements an interface known by another application Y, to have its class definition determined (and perhaps any non-built-in class definitions that it references) and its object state to be sent to application Y and have the methoid defined in the interface invoked.
0
Comment
Question by:NetworkArchitek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 200 total points
ID: 18831347
unfortunately yes, you need the class to be present on both sides (client as well as server), if it has to be loaded at both the ends. However, you can still make your source code generic and agnostic of the class by making them depend on MyRunnable. Read this to get an idea of a sample implementation:

http://www.experts-exchange.com/Programming/Languages/Java/Q_21666226.html#15495977

All you need to do is ensure the JARs are present in the CLASSPATH on both sides, client as well as server.
0
 
LVL 3

Accepted Solution

by:
cavey_79 earned 300 total points
ID: 18836872
You could get away with having the class only on one side. You could then send it over to the other side and dynamically load the class. Once that's done, you could play around with the class via reflection. You could even just create a new instance via reflection, cast it to your interface and then use it directly.

However, from an architecture point of view, I find your approach rather risky. I would rather go with a specified and restricted set of capabilities for the server and the clients would be able to use only those defined operations.

Getting the class file over the wire should be pretty simple (locating it might be a tad more complicated) and then loading it would be done with ClassLoader.defineClass (just get the classloader from one of the existing classes). Then on the newly created Class you would call newInstance, cast it to your interface and you're done. You just need to make sure you know how to create a new instance (for instance mandating that your clients have a default no-arg constructor).
0
 
LVL 10

Author Comment

by:NetworkArchitek
ID: 19169596
Whoops, forgot about this question. Both of you were very helpful on this issue. Thanks.
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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