Java运行时错误 - java.lang.NoClassDefFoundError名称错误

时间:2013-08-07 07:29:45

标签: java exception

当我尝试从命令行运行代码时,出现以下异常错误;

    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();


    }


}

项目结构截图; Project Structure

但是,当我从NetBeans中运行它时,它运行正常吗?

任何帮助将不胜感激!

由于

5 个答案:

答案 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)

  1. 在您的类路径中有.并在X:\User temp\Tom Sothcott执行java命令,或者在您的类路径中直接X:\User temp\Tom Sothcott
  2. 执行java httpclient.httpclient_main 10 10
  3. 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.