当我尝试从命令行运行代码时,出现以下异常错误;
X:\User temp\httpclient>java httpclient_main 10 10
Exception in thread "main" java.lang.NoClassDefFoundError: httpclient_main (wron
g name: httpclient/httpclient_main)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
httpclient_main.java;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package httpclient;
import java.io.DataInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
/**
*
* @author tsothcott
*/
public class httpclient_main {
protected String host;
protected String file;
protected int port;
protected DataInputStream in;
protected DataOutputStream out;
static double threadResult;
String get_file()
{
return this.file;
}
DataOutputStream get_outputstream() {
return this.out;
}
public static void main(String[] args) throws IOException {
InputTxt servers = new InputTxt();
threadResult = 0.0D;
SharedCell cell = new SharedCell();
if(args.length <1)
throw new IOException("Usage: HTTPClient URL Number_Thread");
int num_thread = Integer.parseInt(args[0]);
int count_interval = Integer.parseInt(args[1]);
servers.printservers();
Manufacture prod = new Manufacture(cell, num_thread, count_interval);
prod.start();
}
}
项目结构截图;
但是,当我从NetBeans中运行它时,它运行正常吗?
任何帮助将不胜感激!
由于
答案 0 :(得分:1)
当代码的类文件在编译时出现但在运行时找不到时,会出现NoClassDefFoundError。
答案 1 :(得分:1)
根据您发布的命令行输出,您将从内部包httpclient
调用该程序。你现在的目录是
X:\User temp\Tom Sothcott\httpclient
那是错的。您必须从项目的根目录中调用Java程序。在这种情况下,那将是
X:\User temp\Tom Sothcott
当然,正如Ihsan Kocak告诉你的那样,你必须提供完全合格的班级名称。
答案 2 :(得分:0)
java httpclient_main 10 10
这是错误的用法。将此参数与包name.eg。java httpclient.httpclient_main 10 10
答案 3 :(得分:0)
.
并在X:\User temp\Tom Sothcott
执行java命令,或者在您的类路径中直接X:\User temp\Tom Sothcott
java httpclient.httpclient_main 10 10
java
命令期望包含要执行的main方法的类的完全限定类名。
答案 4 :(得分:0)
线程中的异常&#34; main&#34; java.lang.NoClassDefFoundError: httpclient_main
因此,您将其作为java httpclient_main
运行。它期待httpclient_main.clas
没有任何包裹。
班级试图告诉你它有一个package httpclient;
。您需要从包根目录运行它。转到一个文件夹,以便您的类文件夹包含表示包的httpclient
文件夹,然后执行
java httpclient/httpclient_main.