Potrzebuję odpalić zewnętrzny plik .JAR wraz z podanymi argumentami. Rzadko bawię się refleksją, więc poszukałem w internecie paru gotowców i skleiłem z nich to:
protected void runApplication(File file, String[] args) {
try {
JarFile jarFile = new JarFile(file);
Manifest manifest = jarFile.getManifest();
String mainClass = manifest.getMainAttributes().getValue("Main-Class");
jarFile.close();
URL[] url = new URL[] { file.toURI().toURL() };
URLClassLoader child = new URLClassLoader(url, this.getClass().getClassLoader());
Class classToLoad = Class.forName(mainClass, true, child);
Method method = classToLoad.getDeclaredMethod("main", String[].class);
method.invoke(null, args);
}
//obsługa wyjątków
}
Zwraca mi to java.lang.IllegalArgumentException: argument type mismatch
, mimo, że obydwie linijki poniżej wyświetlają [Ljava.lang.String;
, a więc tablicę Stringów:
System.out.println(args.getClass().getName());
for (Class c : method.getParameterTypes()) {
System.out.println(c.getName());
}
Czy mógłby mi ktoś podpowiedzieć, co robię źle?