How to load a class dynamically and then a call a method?
Hi
I want to call a method from a class,which class is loaded dynamically.
Consider a classA and ClassB..
ClassB contains a method showvalue() which returns a String value.
I want to load a ClassB dynamically in ClassA,and call the method showvalue() and print the returned value of that method (showvalue).
How to do this?
Thanks
Since you found your way to Reflections and Reference Objects, I can only assume you know that reflection is the answer. Since the reflection tutorial on this site, and indeed, the many others on the web, can explain this a whole lot better and more consisely than can be done in a forum, I'll point you in that direction instead. As a starting point, and to show I'm not just fobbing you off, you're interested in the classes java.lang.Class, java.lang.reflect.Method, and the method Class.getMethod(String, Class[])
Similar Messages
-
How to load a class dynamically in the current/system class loader
I need to dynamically load a new jdbc driver jar to the current/system class loader... Please note that creating a new classloader will not help since the DriverManager refers to the systemclassloader itself.
Restarting the application by appending the jar to its classpath will solve the problem but I want to avoid doing this.Did you then create a ClassLoader to load the JDBC
driver and then install it into the system as
directed by the JDBC specification (ie
Class.forName(someClassName))?
And then try to use it from a class loaded fromsome
other ClassLoader (i.e. the system class loader)?
If you did not try this please explain why not.O.K. I just looked at the source to
java.sql.DriverManager. I did not know what I was
talking about, as what I suggested above will not
work.
This is my new Idea:
Create a URLClassLoader to load the JDBC driver also
in this ClassLoader you need to place a helper class
that does the following:
public class Helper {
public Driver getJDBCDriver(String driverClassName,
String url) {
try {
Class.forName(driverClassName);
Driver d = DriverManager.getDriver(url);
return d;
catch(Exception ex) {
ex.printStackTrace();
return null;
}Now create an instance of the Helper class in the new
ClassLoader, and call its getJDBCDriver method to get
an instance of the driver (you will probably have to
create an interface in the root class loader that the
Helper implements so that you can easily call it).
Now from the root classloader you can make calls
directly to the returned Driver and bypass the
DriverManager and its restrictions on cross
ClassLoader access.
The only catch here is that you would have to call to
the returned Driver directly and not use the Driver
Manager.This sounds like will work but I did not want to load DriverManager in a new classloader.. I did a hack
I unzip the jar dynamically in a previously known location (which I included in my classpath when launching the app). The classLoader finds the class now though it did not exist when the app was launched !
A hack of-course but works eh .. -
How to load a Class Dynamically?
hi,
I have the following problem.I am trying to load a class dynamically.For this I am using ClassLoader and its Loadclass method.My code is like this,
File file = filechooser.getSelectedFile();
ClassLoader Cload = this.getClass().getClassLoader();
String tempClsname= file.getName();
Class cd =Cload.loadClass(tempClsname);
Object ob =(Object)cd.newInstance();
showMethods(ob);
In showMethods what i am doing is getting the public methods of the dynamically loaded class,
void showMethods(Object o){
Class c = o.getClass();
System.out.println(c.getName());
vecList = new Vector();
Method theMethods[] = c.getDeclaredMethods();
for (int i = 0; i < theMethods.length; i++) {
if(theMethods.getModifiers()==java.lang.reflect.Modifier.PUBLIC)
String methodString = theMethods.getName();
System.out.println(methodString);
vecList.addElement(methodString);
allmthdlst.setListData(vecList);
Now whenever i work with this i m getting a runtime error of CLASS NOT FOUND Exception..I know its because of Classpath..But i don't know how to resolve it??pls help me in this regard...
Also previously this code was working with java files in the directory in which this java file was present..How to make it work for java file in some other directory..pls help me in this regard...
Thanks in advance..You sure didn't need to post this twice.
http://forum.java.sun.com/thread.jsp?thread=522234&forum=31&message=2498659
When you post code, please use [code] and [/code] tags as described in Formatting Help on the message entry page. It makes it much easier to read and prevents accidental markup from array indices like [i].
You resolve this problem by ensuring the class is in the classpath and you refer to it by its full name.
§ -
How to load a class dynamically (via reflection) in a jsf-component
Hi all,
I am writing my own jsf component and I would like to do it generically. Therefore I have an attribute, where the developer can pass a fully qualified classname, which I want to use to instantiate. But I have a Problem with the classloaders, everytime I get a ClassNotFound-Exception during debugging.
Does anybody know how it is possible, to to get the most parent classloader?
Currently I am even not able to load a class, which is in the same package like all other compontent-classes.
Thank you very much in advance
ThomasWithin web applications, I believe it is recommended to use Thread.getContextClassLoader(). Keep in mind that web applications require different classloader semantics than regular Java applications. The class loader which gets resources from the WAR is favored over others, even when this violates the normal class loading conventions.
-
How to load a class in lib/file.jar
Hi,
I use jboss 4.0.2. and i have a jar-file from a third party in my server/default/lib that calls Thread.currentThread().getContextClassLoader().loadClass("foo"); My problem is that foo.class is never found and i dont know where i have to place this class. I would like to have a seperate folder for my classes, but i dont know how i should configure the classloader to find my classes. i dont want to package them in a jar.
Thanks a lot!i tried this but the class was not found
my simple question is:
if a class in a jar that is placed in my jboss/server/default/lib-directory tries to load a class dynamic, where is it(classloader) looking? please tell me...
thanks -
How can i hide a JFrame and then Show it again in runtime
How can i hide a JFrame and then Show it again in runtime??
Please, please help me
Its URGENTHere's even an example:
import javax.swing.*;
public class HideAndShow extends JFrame
public HideAndShow()
super("Hello");
setSize(200, 200);
setVisible(true);
try
Thread.sleep(2000);
} catch(InterruptedException e) {}
setVisible(false);
try
Thread.sleep(2000);
} catch(InterruptedException e) {}
setVisible(true);
public static void main(String[] args)
new HideAndShow();
The JFrame will show, then hide, then show again. This is at runtime. -
Hello Friends,
I need a help regarding how to load a class manually.
i have the class name -- TestClass1
and Path - com.apps.classes
Thank youThank You------
I got the result
String classPath = com.apps.className;
Object obj = Class.forName(classPath).newInstance(); -
How to load java class from jsp page?
hi all!
Does anyone know how to load java class from jsp page?
I try to load java class from jsp page.
Is it possible to load java class fom jsp page?
thanks and have a good day!What I mean is How to load/open java class file from jsp page?
I think we can open Applet from jsp page by using
<applet code=helloApplet.class width=100 height=100>
</applet>
but, how to open java class which is an application made by Frame?
thanks and have a good day -
Adding prompts dynamically and then send the results to users via email
Hi,
We have to schedule the reports for auto delivery to users at a specific time period. Please, tell me the mechanism to schedule them and how to assign prompt values to them. Currently, the reports are run by users by selecting prompts from the dashboard. If we schedule them, we will have to add some of these prompts dynamically and then send the results to users via email.
Thanks in advance!Hi,
I haven't tested it myself...but may be the following steps can help you.
Fill some session variables depending on the the name of the user who logs in (this can be done easily be creating different session variables and put some executing order in it)
Then you use these session variables as default values for your prompts. So the report will be executed making use of this default values.
And by scheduling your ibot..you can send it by mail.
I don't know if it's this what you're looking for...probably you have to base your prompt values depending on the users that are selected in your ibot?
Hope it can help y ou in some way or another...
KR,
A -
Is there any way of getting rid of the 16,000 e mail on my 4s by load onto the computer and then deleting all. The phone only allows you to delete one at a time.
Delete the account and all the emails will go with it.
-
How to open a pdf file and then attach it with images
I am new to Indesign Server.
I'm currently working on a pdf.
I have a white blank pdf template.
that I want to attach/glue it with images.
How to open a pdf file and then attach it with images.
Please, help me.
Thanks.First step would be to make yourself familiar with InDesign desktop version.
Whatever you intend to achieve, do it there manually. (see regular app docs or forums)
Then try to automate your steps with scripting (see scripting docs or forum)
If you can do it with a script in the desktop version, that script will likely also run in ID Server. (see server forum).
If you can specify missing features not achievable thru scripting or manual use, reconsider to write a plugin (this forum).
A seasoned C++ programmer will need a few months to learn the basics, wade thru tons of documentation etc. Alternatively consider to hire a consultant to do the development work for you.
Dirk -
Trying to download photoshop but when I sign in it loads for a second and then says "You've been signed out".
You are no longer signed into your Creative Cloud applications
-
How do I add a shape and then add gradient inside of it ?
how do I add a shape and then add gradient inside of it ?
Reader commenting and markup tools do not support gradients.
-
How do I create a signature,and then add it to an e ailed document?
How can I create a signature and then use it to sign a document in e mail?
Not sure I understand the question correctly.
Open Mail and click on Preferences under the Mail menu. Click on the Signatures icon in the toolbar. You can set up your signatures there.
But if you are talking about adding a written signature, then you cannot do that within Mail. You would need to scan in an image of your written signature then manually add it to a signature you create in mail as a graphic image. That seems to work. -
How to remove xcode 4.1 and then install xcode 3.2.6?
how to remove xcode 4.1 and then install xcode 3.2.6?
Just an FYI I had xcode 3.2 installed on my snow leopard system and have today just installed Lion and I just cleaned and rebuilt an App I had developed prior to the Lion install to test to see if it would work. I was told it would but wanted to make sure and it successfully rebuilt and works in the simulator.
My issue is a bit different I need to install 4.1 WITHOUT corrupting my 3.2 installation as I may have to go back to rebuild a 3.2 App as we had a fairly unique setup for our xcode. The problem as I see it currently is I do not know how LION installs things in LION xcode is an App downloaded from the App store but I need to be able to download to something other than the default directories and I am not sure if I have that option in LION when installing an App(xcode) from the App Store
Maybe you are looking for
-
IChat's Video CHAT DOESN'T WORK!!!!
I got my iMac in April 2007 and the video chat hasn't EVER worked!!!! I took it to the Apple store three times, but that was no help whatsoever! Please help me! One of the reasons that pushed me to get this over a PC was how well the video cat works!
-
I have tried restarting my computer, creating a new places database (there is no FF profile in Application Support so cannot create a new one), and restoring bookmark back-ups, none of which have resolved the problem.
-
GR based IV" then in MIRO the Cenvat clearing A/c is not getting picked up
If we have creating a PO without checking "GR based IV" then in MIRO the Cenvat clearing A/c is not getting picked up, due to this we are not able to create MIRO document with Excise duty value. We are using Tax calculation procedure TAXINN. Regards,
-
Validation Condition: value zero
Dear Experts, we try to find a way to set up a validation wich checks: if the value at item 123 is greater than zero ==> than the value at item 456 should be greater than zero as well. We thought about a condition but we cannot set up a condition, th
-
Cisco ISE being down alsmost every day for approx. a minute
Hi, our monitoring tool says ISE (1.2) is alsmost every night down for about a minute. Mostly it's about 4am (that's where ISE checks if there is something to purge), but mostly it's between 0 am and 4am. Do you have a idea which logs I could check t