简单的问题,怎么调用main方法

jongsuny 2009-03-25 08:30:53
package com.jong.test.reflect;
//第一个类
public class ReflectInvoke {

/**
* @param args
*/
public static void main(String[] args) {
System.out.println("It works!!");
}
public void print(){
System.out.println("print method!");
}
@Override
public String toString() {
return "ReflectInvoke";
}


}
//第2个类

public class InvokeMainMethod {

/**
* @param args
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
String className="com.jong.test.reflect.ReflectInvoke";
Class clazz=null;
try {
clazz=Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Object o=clazz.newInstance();
System.out.println(o.toString());
System.out.println(clazz.getClass());
ReflectInvoke.main(new String[]{});
Class[] arg=new Class[]{String.class};
//形式参数,要调用对象的形参
Object[] args1=new Object[]{};
//实参数,要调用对象的实参
InvokeMainMethod.method(o,"main");
try {
InvokeMainMethod.forname(o, "print", null,null);
} catch(Exception e){
System.out.println(e);
}
}
public static void forname(Object o,String methodname,Class[] arg,Object[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Class c=o.getClass();
Method m=c.getMethod(methodname,arg);//获得方法。
System.out.println(m.getName());
Object obj=m.invoke(o, args);
System.out.println(o.toString());
}
public static void method(Object o,String method){
Class c=o.getClass();
try {
Method m=c.getMethod(method, new Class[]{});
Object obj=m.invoke(o, null);
System.out.println(obj);
} catch (Exception e) {
System.out.println(e);
}

}

}


在第2个类里怎么调用第一个类的main方法,像第一个类的print方法是没有问题的,
关键是怎么获得String类型的数组?请教高手
...全文
332 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jongsuny 2009-03-25
  • 打赏
  • 举报
回复
try {
ReflectInvoke.class.getMethod("main", new Class[]{String[].class}).invoke(null,new Object[]{null});
// InvokeMainMethod.forname2(o, "main");
} catch(Exception e){
System.out.println(e);
}
可以这样做了...

Class clazz=new Class[]{String[].class};

这里没想到有String[]可以代表...String 数组...4555555555555
jerry313 2009-03-25
  • 打赏
  • 举报
回复
没看懂LZ的问题,能解释下么
hoojo 2009-03-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jongsuny 的回复:]
我的问题是怎么样才能获得String类型的数组呢?

Java code
package com.jong.test.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class InvokeMainMethod {

/**
* @param args
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void main(String[] args) throws Instantiat…
[/Quote]
同意此观点
一般是不调用main 函数
因为它是主函数
如果要调用,上面的方法可以
^_*
ghq625 2009-03-25
  • 打赏
  • 举报
回复
格式不好看哈
jongsuny 2009-03-25
  • 打赏
  • 举报
回复
我的问题是怎么样才能获得String类型的数组呢?

package com.jong.test.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class InvokeMainMethod {

/**
* @param args
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
String className="com.jong.test.reflect.ReflectInvoke";
Class clazz=null;
try {
clazz=Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Object o=clazz.newInstance();
System.out.println("========================");

System.out.println(o.toString());
// clazz.getMethod("main", parameterTypes)
System.out.println(clazz.getClass());
System.out.println("========================");

ReflectInvoke.main(new String[]{});
Class[] arg=new Class[]{};
这里怎么获得String类型的数组????
         Object[] args1=new Object[]{};
//实参数,要调用对象的实参
InvokeMainMethod.method(o,"main");
System.out.println("========================");
try {
InvokeMainMethod.forname(o, "print", null,null);
System.out.println("========================");
InvokeMainMethod.forname(o, "main",arg,args1);
System.out.println("========================");
InvokeMainMethod.invokeMethod(o, "main",new Object[]{"a","b"});
InvokeMainMethod.invokeMethod(o, "main",null);

System.out.println("========================");


} catch(Exception e){
System.out.println(e);
}
}
public static Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {
Class ownerClass = owner.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(owner, args);
}

}


dreamhunter_lan 2009-03-25
  • 打赏
  • 举报
回复
main方法是静态方法,跟调用其他类的static方法一样
都是类名.main(args),args就是String类型的数组,从命令行传入的
(如果用IDE的就自己去网上找看怎么输入,讲了太多次了不想讲了)
还有就是以后代码别这样贴了,比较难看(我没看!)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧