lundi 29 février 2016

IllegealArgumentException on invokation of specific method

Ineed to implement a script that calsl a method from a jar-file at runtime. My Code so far looks like this:

package com;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class MyJarImport {

    public static void main(String[] args) throws Exception{

        // C:\\KnimeIntegrationProjekt\\Knime\\pmmlj
        // C:\\Users\\marcel.hoffmann\\JavaWorkspace\\HelloWorld\\bin\\Hello
        //C:\\Knime\\ClusterJar

    File file  = new File("C:\\Knime\\ClusterJar");
    try{
    URL url = file.toURI().toURL();
    URL[] urls = new URL[]{url};
    URLClassLoader cl = new URLClassLoader(urls);

    //loading the jar file from directory
    Class<?> cls = cl.loadClass("MainModel");

    Constructor<?> constructor = cls.getConstructor();
    //instantiate the classfile
    Object MainModelObj = constructor.newInstance();
    /* searching the methods in order to get an instance*/
    Method mGetInput = cls.getMethod("getInputFields");
    Method mNumInputs = cls.getMethod("getNumInputs");
    int dim = (int) mNumInputs.invoke(MainModelObj);

    Method mMining = cls.getMethod("getMiningFunction");
    String miningFunction = "";
    /*returns either Classification, Regression or Clustering
    not used yet, but maybe later to figure out what miningfunction has been used
    that effects the content of evalOutputs[]*/
    miningFunction = (String)mMining.invoke(MainModelObj);
    String[] inputFieldUnsorted = new String[dim];
    inputFieldUnsorted = (String[]) mGetInput.invoke(MainModelObj);
    Object[] sortedInputs = new Object[dim];

    int index = 0;
    /*sorting the inputs from getInputFields respecting the getInputFieldIndex returns*/
    for(int i= 0; i<inputFieldUnsorted.length;i++){
        Method mInputIndex = cls.getMethod("getInputFieldIndex",String.class);
        index = (int) mInputIndex.invoke(MainModelObj, inputFieldUnsorted[i]);
        sortedInputs[index] = inputFieldUnsorted[i];
    }
    /* sortedInputs[] contains the column names of the iris.csv table
invoking the evaluate method. concerning the documentation the method u need to call
in order to use the model inside the jarfile*/
    Method mEvaluate= cls.getMethod("evaluate", Object[].class);
   /* allocating an array for the outputs of the evaluate method*/
    Object[] evalOutputs = new Object[dim];
    evalOutputs = (Object[]) mEvaluate.invoke(MainModelObj, sortedInputs, inputFieldUnsorted);
   /* closing the opened classloader*/
    cl.close();
    } catch(IOException e) {
        System.out.println("File not found!");
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        System.out.println("Class not found!");
    }
    }
}

This Code as it is throws a IllegalArgumentException:wrong number of arguments.

Exception in thread "main" java.lang.IllegalArgumentException: 
wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.MyJarImport.main(MyJarImport.java:58)

I got a documentation about the jar file I need to load and it says, that the method signature is something like

Object[] MainModel.evaluate(Object[] inputs);

I debugged the code and I think everthing works, in terms of the values of the variables. just this call semms the problem. I just read something about invokation, that says the VM tries to invoke the mainmethod of the jar file and the main methods requires its String[] args parameter, but why would it do that, if I want to invoke an explizit method?

I hope someone can help me with this little problem.

Thanks in advance.

cheers





Aucun commentaire:

Enregistrer un commentaire