当前位置: 代码迷 >> Eclipse >> eclipse中CLASS的有关问题
  详细解决方案

eclipse中CLASS的有关问题

热度:47   发布时间:2016-04-23 14:14:02.0
eclipse中CLASS的问题
package check;

import java.lang.reflect.*;

public class CheckMain {

public static void main(String[] args) {
Class item;

try {
if (args.length > 0)
item = Class.forName(args[0]);
else
item = Class.forName("CheckMain");
  // 这里出错,ClassNotFoundException
Method[] methods = item.getDeclaredMethods();
boolean check = false;
for (int i = 0; i < methods.length; i++) {
Method meth = methods[i];
Class returnType = meth.getReturnType();
int mods = meth.getModifiers();
String modifiers = Modifier.toString(mods);
Class[] paramVal = meth.getParameterTypes();
String params = new String();

if (paramVal.length > 0) {
params = paramVal[0].getName();
} else if (paramVal.length > 1)
continue;

if (params.equals("[Ljava.lang.String;")
& returnType.getName() == "void"
& modifiers.equals("public static")
& meth.getName() == "main") {
System.out.println("\nThis is an application!");
check = true;
break;
}
}
if (!check) {
System.out.println("\nThis is not an application!");
}
} catch (ClassNotFoundException e) {
System.out.println("Error is :" + e.toString());
}
}
}

为什么在eclipse中总是抛出java.lang.ClassNotFoundException异常?
而在cmd下就没有问题。
俺刚学java和eclipse,还望各位大侠不吝赐教。谢了先

------解决方案--------------------
你在cmd下是不是传参数了,传的参数能找到类.直接在eclipse下运行默认是没有参数的,所以你就执行了Class.forName("CheckMain"); 


然而你的类的全名应该是check.CheckMain
  相关解决方案