Jump to content
Excelsior Forums
Sign in to follow this  
chandra_excel

getMethod problem in dynamic loading of dll

Recommended Posts

Hi,

I have modified the example codes of ..JET\samples\dll\dynamic.? I get 'no such method' exception when using the new code, compile them using JSE1.5.0 and build them using JET 90-day trial version.? DLL loads fine but method cannot be accessed.

Any help please?? Or is this limitation of trial version?

Codes:

Code for exe:

-------------------

public class DynamicLoad

{

static public void main (String args[]) throws InstantiationException, IllegalAccessException

{

System.out.println ("DLL is not loaded yet.");

System.out.println ("Let's find class (and load DLL)");

try

{

Class c = Class.forName ("ClassFromDLL");

System.out.println ("Class found (DLL is loaded successfully)");

c.newInstance();

// added to example

try

{

Class[] parameterTypes = new Class[] {null};

java.lang.reflect.Method m = c.getMethod("callMe", parameterTypes); // problem is here

}

catch (NoSuchMethodException e2)

{

System.out.println("Method not found. " + e2.toString());

}

}

catch (ClassNotFoundException e)

{

System.out.println ("Class not found. Maybe DLL is absent?");

}

}

}

Code for dll:

---------------

public class ClassFromDLL

{

public ClassFromDLL()

{

System.out.println ("_______Hi from DLL!_______");

}

// new method

public void callMe()

{

System.out.println ("_______Hi from DLL's callMe!_______");

}

}

Many thanks

Chandra

Share this post


Link to post
Share on other sites

This problem is not related to JET. If you run the sample with Sun JRE, you'll see the same error.

To fix the problem, just replace the line

            Class[] parameterTypes = new Class[] {null};

with

            Class[] parameterTypes = new Class[] {};

in the DynamicLoad class source.

Share this post


Link to post
Share on other sites

Thank you very much.? Removing 'null' works perfect.?

Original purpose was indeed trying to load the dll dynamically and invoke some method in it (here, it is 'callMe').? I added the following lines after getting the method:

Object[] arguments = new Object[] {this};

try

{

m.invoke(c, arguments);

}

catch (Exception e3) // few exceptions seems to occur, so using general 'Exception'

{

System.out.println("Method found. But invoking throws: " + e3.toString());

}

As expected, using "this" causes a non-static variable being referenced and compiling itself does not finish.?

I see this is not JET problem, however a hint might help.? Will explore more on Java groups mean-while.

Thanks,

Chandra.

Share this post


Link to post
Share on other sites

You need need to pass an instance of the class to the m.invoke() call.

So, first just replace the line

? ? ? ? ?c.newInstance();

with

? ? ? ? ?Object instance = c.newInstance();

Second, replace the call

? ? ? ? ?m.invoke(c, arguments);

with

? ? ? ? ?m.invoke(instance, arguments);

or just

? ? ? ? ?m.invoke(instance);

Share this post


Link to post
Share on other sites

With the changes you suggested, the code works neat and I get the method invoked just fine.? (I have removed the line "Objects[] arguments..." and used "m.invoke(instance)" for testing.? Thank you.

Passing some parameter types instead of null/nothing in the case of Class[] parameterTypes and using arguments like "this" or "someClassName.class" instead of null/nothing while passing arguments, are my next tasks.? Will explore on that and come back later.? Compilation of such code works fine now in normal Java compiling but not after JET exe is built.? Will post about it under a separate topic more clearly.

Many thanks again,

Chandra.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×