很久没在命令行运行Java了,今天突然要运行一个觉得是在是生疏,虽然之前已有同样的经过,可惜没记录下来,吃一堑长一智,这次必须得记录下来了。
我首先遇到的第一个问题是编码问题,因为我是在eclipse上写好的一个测试类ApacheStatusStatistic.java,并且能在eclipse上能正确运行。但是当我把这个文件拷贝文件 E:\test目录下,并在命令行执行时,其报“需要为 class,interface或enum....”错误,详情如下:
经多翻查找,终于知道其为编码问题,原来在ecplise环境中的编码是UTF-8,改为ANSI就解决了。
遇到的第二个问题是编译运行的问题。我的类如下:
Java代码
packageapache;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.net.MalformedURLException;
importjava.util.HashMap;
importjava.util.Map;
publicclassApacheStatusStatistic {
/**
* @param args
*/
publicstaticvoidmain(String[] args) {
try{
java.net.URL url =newjava.net.URL("http://www.apache.org/server-status?auto");
BufferedReader in =newBufferedReader(newInputStreamReader(url.openStream()));
String open ="Open";
String waiting ="Waiting";
String readingRequest ="Reading request";
String sendingReply ="Sending reply";
String keepingAlive ="Keeping alive";
String dnsLookup ="DNS lookup";
String logging ="Logging";
// 额外的
String startingUp ="Starting up";
String closingConnection ="Closing connection";
String gracefullyFinishing ="Gracefully finishing";
String idleCleanupofWorker ="Idle cleanup of worker";
Map data =newHashMap();
data.put(open,0);
data.put(waiting,0);
data.put(readingRequest,0);
data.put(sendingReply,0);
data.put(keepingAlive,0);
data.put(dnsLookup,0);
data.put(logging,0);
// 额外的
data.put(startingUp,0);
data.put(closingConnection,0);
data.put(gracefullyFinishing,0);
data.put(idleCleanupofWorker,0);
String str;
System.out.println("基本统计数据如下:");
while((str = in.readLine()) !=null) {
if(str.startsWith("Scoreboard:")) {
String scoreboard = str.substring(11);
// System.out.println(scoreboard);
for(inti =0; i
charc = scoreboard.charAt(i);
switch(c) {
case'_':
data.put(waiting, data.get(waiting) +1);
break;
case'S':
data.put(startingUp, data.get(startingUp) +1);
break;
case'R':
data.put(readingRequest, data.get(readingRequest) +1);
break;
case'W':
data.put(sendingReply, data.get(sendingReply) +1);
break;
case'K':
data.put(keepingAlive, data.get(keepingAlive) +1);
break;
case'D':
data.put(dnsLookup, data.get(dnsLookup) +1);
break;
case'C':
data.put(closingConnection, data.get(closingConnection) +1);
break;
case'L':
data.put(logging, data.get(logging) +1);
break;
case'G':
data.put(gracefullyFinishing, data.get(gracefullyFinishing) +1);
break;
case'I':
data.put(idleCleanupofWorker, data.get(idleCleanupofWorker) +1);
break;
/** Open slot with no current process */
case'.':
data.put(open, data.get(open) +1);
break;
}
}
}else{
System.out.println(str);
}
}
System.out.println("状态统计数据如下:");
System.out.println(open +":"+ data.get(open));
System.out.println(waiting +":"+ data.get(waiting));
System.out.println(readingRequest +":"+ data.get(readingRequest));
System.out.println(sendingReply +":"+ data.get(sendingReply));
System.out.println(keepingAlive +":"+ data.get(keepingAlive));
System.out.println(dnsLookup +":"+ data.get(dnsLookup));
System.out.println(logging +":"+ data.get(logging));
/** 额外的*/
System.out.println(startingUp +":"+ data.get(startingUp));
System.out.println(closingConnection +":"+ data.get(closingConnection));
System.out.println(gracefullyFinishing +":"+ data.get(gracefullyFinishing));
System.out.println(idleCleanupofWorker +":"+ data.get(idleCleanupofWorker));
in.close();
}catch(MalformedURLException e) {
}catch(IOException ee) {
}
}
}
我编译并运行,但结果却是“no class def found error”,详情如下:
后来经过调查时,运行编译好的文件时,必须进入其所在的目录如下图所示:
但我运行时却报:Exception in thread "main" java.lang.NoClassDefFoundError: ApacheStatusStatistic (wrong name: apache/ApacheStatusStatistic),详情如下:
经过调查因为包的问题,如果Java类有包名,你必须建相应的包文件夹并把文件编译或拷贝到到相应的文件夹下,如这里
我得在E:\test下创建文件夹apache并把ApacheStatusStatistic.class拷贝到这个文件夹下。并且必须在命令行指定包名,执行如下才成功运行:
终于搞定,铭刻于心呀
—— 摘自,iteye(拓梦人)