0

When I try to run my code from the command line, I get the following exception error;

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


    }


}

Screenshot of project structure; Project Structure

Howewver, when I run it from inside NetBeans, it runs fine?

Any help would be appreciated!

Thanks

Jake Evans
  • 978
  • 5
  • 13
  • 33

5 Answers5

1

NoClassDefFoundError comes when class file of your code is present at compile time but not found at runtime.

manoj
  • 118
  • 8
1

According to your posted command line output, you are invoking the program from inside the package httpclient. Your current dir is

X:\User temp\Tom Sothcott\httpclient

That is wrong. You must invoke your Java programs from the project's root directory. In this case, that will be

X:\User temp\Tom Sothcott

And of course, you must provide the fully qualified class name, as Ihsan Kocak told you.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
0
java httpclient_main 10 10

It is wrong usage.Use this parameter with package name.eg.java httpclient.httpclient_main 10 10

ihsan kocak
  • 1,541
  • 1
  • 17
  • 26
  • Now it won't even run; `Error: Could not find or load main class httpclient.httpclient_main` – Jake Evans Aug 07 '13 at 07:44
  • You can try these two when you are getting the error: 'could not find or load main class' If your class file is saved in following directory with HelloWorld program name d:\sample 1.java -cp d:\sample HelloWorld 2.java -cp . HelloWorld – ihsan kocak Aug 07 '13 at 07:53
0
  1. Have . in your classpath and execute the java command at X:\User temp\Tom Sothcott, OR, have X:\User temp\Tom Sothcott directly in your classpath
  2. Execute by java httpclient.httpclient_main 10 10

java command is expecting the fully qualified classname for the class containing the main method to execute.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
0

Exception in thread "main" java.lang.NoClassDefFoundError: httpclient_main

So, you ran it as java httpclient_main. It's expecting a httpclient_main.class without any package.

The class is trying to tell you that it has a package httpclient;. You need to run it from the package root on. Go one folder up so that your class folder contains the httpclient folder representing the package and then execute

java httpclient/httpclient_main.
Sandeep Roniyaar
  • 204
  • 1
  • 8
  • 26