The Artima Developer Community
Sponsored Link

Java Answers Forum
dinamiclly loaded class method calling

1 reply on 1 page. Most recent reply: Nov 8, 2005 3:03 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Marius Jursys

Posts: 3
Nickname: shadow3d
Registered: Nov, 2005

dinamiclly loaded class method calling Posted: Nov 3, 2005 3:36 PM
Reply to this message Reply
Advertisement
Okey, I will load classes with applet, I have c100.class, c101.class and so on class files, thei all contain one class as follows:

public class <filename>{ // <filename> - wouldbe c100, c101 and so on
public void function(char a[], char b[], int c, int d, int e[]){
//some code
};
}

So I need dinamiclly loading one class at the time and I use code below, where name - is class name(c100, c101), and surl - path were classes are stored:

public Class getClass(String name, String surl) {
URL url = null;
URLClassLoader ucl = null;
try {
url = new File(surl).toURL();
URL urls[] = new URL[] {
url};
ucl = new URLClassLoader(urls);
}
catch (MalformedURLException ex) {
}
if (ucl != null) {
try {
return ucl.loadClass(name);
}
catch (ClassNotFoundException ex1) {
return null;
}
}
return null;
}

So for now everything is fine, but now then I load class in lets say main function:

void static main(){
Class myClass = getClass("c100", "some dir");
//here I need to call function(char[],char[],int,int,int[]);
}

the problem is that I dont know how to call function. I have find out how to get that function:

Method m = myClass.getMethods[0];

Is where any way to cast as somthing :

Object object = myClass.newInstance();
object.getClassName() o = (object.getClassName())object;
// and then calling function\
o.function(char[], char[], int, int, int[]);

Any help on how I can make use of it now ?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: dinamiclly loaded class method calling Posted: Nov 8, 2005 3:03 AM
Reply to this message Reply
Use
getConstructor(Class parametertypes ...).newInstance(Object parameters ...)
to create a new instance of the object.


You can also retrieve static methods using
getMethod(String name, Class parameters ...)

Flat View: This topic has 1 reply on 1 page
Topic: Arrays Previous Topic   Next Topic Topic: Problem to Connect JMXConnectorServer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use