日期:2014-05-20  浏览次数:20831 次

java.lang.NoSuchMethodError: main 错误,请教!
java.lang.NoSuchMethodError: main
Exception in thread "main" 
---------------------------------代码如下
Java code

import java.io.*; 
  public class Util extends Object
  {     
    public static int RunThis(String args)
     {
       Runtime rt = Runtime.getRuntime();
       int rc = -1;
       try
       {
       Process p = rt.exec(args);
       int bufSize = 4096;
       BufferedInputStream bis =
       new BufferedInputStream(p.getInputStream(), bufSize);
       int len;
       byte buffer[] = new byte[bufSize];
     
       while ((len = bis.read(buffer, 0, bufSize)) != -1)
         System.out.write(buffer, 0, len);
          rc = p.waitFor();
          }
         catch (Exception e)
         {
           e.printStackTrace();
           rc = -1;
         }
       finally
       {
         return rc ;    
       }
      } 
 }






------解决方案--------------------
没有main函数,运行需要main 函数
------解决方案--------------------
内容太少,不知道具体哪里错了!多贴些东西出来!
------解决方案--------------------
java.lang.NoSuchMethodError: main 
Exception in thread "main"
没有main函数
直接运行
main函数是程序的入口


------解决方案--------------------
是什么程序啊?调试了~不知道你要输入些什么东西啊?
------解决方案--------------------
Util , 看名字感觉这不像是程序的入口,LZ应该把用Util 的类贴上来呀。
------解决方案--------------------
据我夜观天象,楼主贴的类实现如下功能:调用一个可执行程序,捕捉该程序的标准输出并用System.out显示出来直到该程序结束

但是楼主试图运行这个类的时候,类文件里面没有提供main入口函数,所以报了java.lang.NoSuchMethodError: main 
Exception in thread "main"

原因大家都知道,运行一个Java类文件,需要该类文件提供public static void main(String [] args)的入口函数

其实程序本身没有问题,但是这段代码只是一个工具类,还需要另外一个类来调用RunThis这个静态函数,为了简单起见,我把入口函数直接写到Util类文件里,获得的代码如下

import java.io.*;

public class Util extends Object {

public static void main(String [] args) {
if (args.length >= 1) {
int r = Util.RunThis(args[0]);
System.out.println("Command executed, return code = " + r);
} else {
System.out.println("Usage: java Util <command1> <command2> ... <commandN>");
}
}

public static int RunThis(String args) {
Runtime rt = Runtime.getRuntime();
int rc = -1;
try {
Process p = rt.exec(args);
int bufSize = 4096;
BufferedInputStream bis = new BufferedInputStream(p
.getInputStream(), bufSize);
int len;
byte buffer[] = new byte[bufSize];

while ((len = bis.read(buffer, 0, bufSize)) != -1)
System.out.write(buffer, 0, len);
rc = p.waitFor();
} catch (Exception e) {
e.printStackTrace();
rc = -1;
}
return rc;
}
}

然后随便写一个批处理文件比如1.bat,在命令行执行:java Util 1.bat
运行成功
------解决方案--------------------
是啊 
本来是没有问题的啊

但是LZ是在编译的时候没有问题

运行时就有这个异常的

这个类本来就不是来运行的

而是一个javavbean 

被其他的类 方法来调用的

LZ不需要去运行这个程序

编译就可以了
------解决方案--------------------
没有MAIN函数.....
可以按照9楼的去做把
------解决方案--------------------
没有MAIN函数
------解决方案--------------------