1

I'm trying to call method from .jar

But when I want to declare array of params for getDeclaredMethod I've got an error "cannot find symbol" on

Class[] params = new Class[]{ String };
                              ^

Whole method

 public void CallCloseWindow(String title){
        //export dll
        InputStream in = staticapi.class.getResourceAsStream("CloseWindow.dll"); 
        File fileOut = new File(System.getProperty("C:\\Java\\CloseWindow.dll")); 
        OutputStream out = new FileOutputStream(fileOut); 
        int c;
        while ((c = in.read()) != -1) { 
            out.write(c);
        }
        in.close(); 
        out.close(); 
        //call from jar class
        Class[] params = new Class[]{ String };
        Object[] parms = new Object[] { new String(title) };
        URL url=new URL("jar:file:/callapi.jar/");
        URLClassLoader ucl = new URLClassLoader(new URL[] { url });
        Class obj = Class.forName("callapi.callapi", true, ucl);
        Method m = obj.getDeclaredMethod("CloseWindow",params);
        Object instance = obj.newInstance();
        Object result = m.invoke(instance,parms);
    }

What's the problem?String.TYPE doesn't work either

3 Answers 3

4

Try with Class[] params = new Class[]{ String.class };

Sign up to request clarification or add additional context in comments.

5 Comments

This is Java? What kind of construct is that - new Class[]{ String.class }
Yes it's Java, you are constructing a Class array with only one element (the String.class).
@Adel: This is perfectly standard Java (but the bit you've quoted is incomplete and makes no sense without the new).
@Fido - Thank You Very Much, that's pretty powerful.
I was waiting for several minutes but then forgot about it.
1

try:

Class[] params = new Class[]{ String.class };

Comments

1

As others have pointed out, you just missed the .class. No need to call new when initializing an array on declaration though:

Class[] params = { String.class };

should do fine. (It is only if you reinitialize the array later on, or if you for instance create an anonymous array you would need new.)

In fact, you don't need to create the params array at all. Since Class.getDeclaredMethods is a vararg method you could just do

Method m = obj.getDeclaredMethod("CloseWindow", String.class);
                                                \__________/
                                                     |
        this will be transformed to a Class[] on the fly by the constructor

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.