Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading and instantiating a remote class?

 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Can any one please explain how to load and instantiate a class which is located on a different machine.

I tried this code but it throws ClassNotFoundException.



I have Test1 class at file://192.168.1.3/java/operators location. I am able to access it from windows explorer. 192.168.1.3 is the ip address of the m/c where Test1 class file is placed.

Can't I use file protocol here.

Here is the stack trace.

java.lang.ClassNotFoundException: Test1
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at Test.main(Test.java:7)


Thanks & Regards


Naseem
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't call a method on a class that isn't in the same JVM.

Have you tried RMI?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naseem --

You're actually on the right track, but I would guess that the problem is that that's not really a valid URL; it's a Windows fake file-sharing thingamabob. If the class file were served by an actual web server so that you could get it via an http:// URL, you should be able to get this to work.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Friedman. Thanks Aviroop.

Yeah it worked as said when I accessed it from tomcat servlet container.



Here is my Hello Bean class




Thanks & Regards


Naseem
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting the code Naseem.

Thanks Friedman. Thanks Aviroop.

By the way, who is Aviroop?
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried out the above program. However, I am getting the following runtime error.

Exception in thread "main" java.lang.NoClassDefFoundError: Test (wrong name: tes
t)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)


I placed the Hello.class file in the MyExamples folder which is inside the webapps folder. There is no problem with the installation of the Apache Tomcat webserver. What's amiss here?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic