你是说Main函数,还是main()函数?
如果Main,那和一般的函数没区别,如果是main(),则不能直接调用。但可以通过java的Runtime类去执行。
比如你的D:盘根下有个java类 Hello.class,里面有main()函数
你的执行类是TestClass
public class TestClass {
public static void main(String argv []) {
String cmdstr = "cmd /c java d:/Hello";
Runtime run = Runtime.getRuntime();
try {
Process p = run.exec(cmdstr);
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
String lineStr;
while ((lineStr = inBr.readLine()) != null)
System.out.println(lineStr);
if (p.waitFor() != 0) {
if (p.exitValue() == 1)//p.exitValue()==0表示正常结束,1:非正常结束
System.err.println("命令执行失败!");
}
inBr.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
这样去调用。
跟调用普通static方法一样调用
类名.main
public class TestAa {
public static void main(String[] args) throws Exception {
Test.main(args);
}
}
class Test{
public static void main(String[] args) {
System.out.println("hello world");
}
}