Java 调用特定方法时出现IllegealArgumentException
我需要在运行时使用反射对JAR文件中的类调用一个方法 到目前为止,我的代码如下所示:Java 调用特定方法时出现IllegealArgumentException,java,reflection,illegalargumentexception,Java,Reflection,Illegalargumentexception,我需要在运行时使用反射对JAR文件中的类调用一个方法 到目前为止,我的代码如下所示: 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 MyJarI
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!");
}
}
}
我参考了文档,它说方法签名类似于:
Object[] MainModel.evaluate(Object[] inputs);
我调试了代码,从变量的值来看,我认为一切都正常。但这个电话似乎是个问题
我刚刚读了一些关于调用的内容,说VM试图调用jar文件的main方法,main方法需要它的String[]args参数,但是如果我想调用显式方法,它为什么要这样做呢
我希望有人能帮我解决这个小问题。异常消息中的参数数目错误您的签名似乎有误。您正在调用
MainModel.evaluate(类输入)代码>而不是MainModel.evaluate(对象[]输入)代码>。也许可以尝试cls.getMethod(“evaluate”,新对象[]{})
?
Object[] MainModel.evaluate(Object[] inputs);