Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Accessing method of one application from another

 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
This may seem strange, but I want to know if it is possible to access a method in one class from another totally separate application. If I understand correctly, I need to have the instance of the class to use the Method.invoke() call. So, is there a way to get the instance of another class of some application that is already running? Also, I have been doing some experimenting with the reflect API and I do not quite understand what the Class[] is that must be passed to getMethod(String name, Class[] parameterTypes) of the Class class. The docs say it is the formal parameters of the method you want to get. Could someone please explain how this is used to represent the formal parameters? I get a NoSuchMethodException when I run this sample code, I think because "params" is not defined correctly. Anyone that can help me on this matter is greatly appreciated!!
Thanks,
Barry
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,
I had the same problem and I have figured it out. Here is the example I did and it works now. Hope this helps.
Trish
---------------------------------------------------------------
import javax.servlet.http.*;
import javax.servlet.*;
import java.util.*;
import java.io.*;
import java.lang.reflect.Method;
public class test1 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
performTask(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
performTask(request,response);
}

public void performTask(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
PrintWriter out = null;
response.setContentType(CONTENT_TYPE);
out = response.getWriter();
out.println("test");
String a = "help";
try {
Class classObj = Class.forName("test1");
Class[] params = new Class[3];
params[0] = Integer.TYPE;
params[1] = a.getClass();
params[2] = out.getClass();
Method method = classObj.getMethod("testMethod",params);
test1 obj2 = new test1();
Object[] args = new Object[3];
args[0] = new Integer(10);
args[1] = a;
args[2] = out;
Object retObj = method.invoke(obj2,args);
} catch (Exception e) {
out.println(e);
}
out.println("performTask:here ");
}
public void testMethod(int i, String a, PrintWriter out) {
out.println(a);
out.println(i);
}
}
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! Thanks for the help!
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know, this IS what RMI and other distributed object technologies are for...
Don't reinvent the wheel!
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic