I m learning java reflection. I tried
System.exit(3); Class.forName("java.lang.System").getMethod("exit", Integer.TYPE).invoke(null, 3);
and it works. I successfully also ran
System.out.println(Class.forName("java.lang.System").getMethod("currentTimeMillis").invoke(null));
Now how can i invoke System.out.println reflectively
java.lang.Class.forName("java.lang.System").getMethod("out.println", String.class).invoke(null, "Hi!");
is giving error. I know System does not have out function. So suggest a way to call System.out.println reflectively
Here is the complete example
public class ReflectionDemo1 {
public static void main(String[] args) throws Exception {
// java.lang.System.exit(3);
// java.lang.Class.forName("java.lang.System").getMethod("exit", Integer.TYPE).invoke(null, 3);
// java.lang.System.currentTimeMillis()
// System.out.println(Class.forName("java.lang.System").getMethod("currentTimeMillis").invoke(null));
// System.out.println("Hi!");
java.lang.Class.forName("java.lang.System").getMethod("out.println", String.class).invoke(null, "Hi!");
}
}