Can anyone help I am trying run this program and I keep on g
Can anyone help, I am trying run this program and I keep on getting an error saying
Note: DynamicMethodInvocation.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
I\'m not sure what to change to fix this error
import java.lang.reflect.*;
import java.lang.Class;
import java.lang.ClassNotFoundException;
import java.lang.InstantiationException;
import static java.lang.System.out;
import static java.lang.System.err;
public class DynamicMethodInvocation {
public void work(int i, String s) {
out.printf(\"Called: i=%d, s=%s%n\ \", i, s);
}
public static void main(String[] args) {
Class clX = null;
Object x = null;
try {
try {
clX = Class.forName(args[0]);
} catch (ClassNotFoundException e) { err.println(e); }
try {
x = clX.newInstance();
} catch (InstantiationException e) { err.println(e); }
} catch (IllegalAccessException e) { err.println(e); }
out.println(\"class of x: \" + clX + \'\ \');
Class[] argTypes = { int.class, String.class };
Method toInvoke = null;
try {
toInvoke = clX.getMethod(\"work\", argTypes);
out.println(\"method found: \" + toInvoke + \'\ \');
} catch (NoSuchMethodException e) {
err.println(e);
}
Object[] theArgs = { 30, \"Banana\" };
try {
toInvoke.invoke(x, theArgs);
} catch (IllegalAccessException e) {
err.println(e);
} catch (InvocationTargetException e) {
err.println(e);
}
}
}
Solution
Hi, FRiend, can you please tell me the exact issue. How you are running what error are you getting.
import java.lang.reflect.*;
import java.lang.Class;
import java.lang.ClassNotFoundException;
import java.lang.InstantiationException;
import static java.lang.System.out;
import static java.lang.System.err;
public class DynamicMethodInvocation {
public void work(int i, String s) {
out.printf(\"Called: i=%d, s=%s%n\ \", i, s);
}
public static void main(String[] args) {
Class<?> clX = null;
Object x = null;
try {
try {
clX = Class.forName(args[0]);
} catch (ClassNotFoundException e) { err.println(e); }
try {
x = clX.newInstance();
} catch (InstantiationException e) { err.println(e); }
} catch (IllegalAccessException e) { err.println(e); }
out.println(\"class of x: \" + clX + \'\ \');
Class[] argTypes = { int.class, String.class };
Method toInvoke = null;
try {
toInvoke = clX.getMethod(\"work\", argTypes);
out.println(\"method found: \" + toInvoke + \'\ \');
} catch (NoSuchMethodException e) {
err.println(e);
}
Object[] theArgs = { 30, \"Banana\" };
try {
toInvoke.invoke(x, theArgs);
} catch (IllegalAccessException e) {
err.println(e);
} catch (InvocationTargetException e) {
err.println(e);
}
}
}


