1

I created a java program in Eclipse. When I run the program in Eclipse ("run as -> Java Application") the program runs fine and I have the correct output. However, when I try to run the program in the command line interface I got this error:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: helloworld/HelloWorld) Could not find the main class: HelloWorld. Program will exit.

The class file is in directory bin and I try to run it with the command:

java HelloWorld
3
  • Is your class in the default package (i.e. no package declaration)? And when saying I try to compile it with the command: java HelloWorld, did you mean running? Commented May 9, 2011 at 9:41
  • Yes I mean running the project not compiling it. Commented May 9, 2011 at 9:52
  • My class have a package declaration: package helloworld; Commented May 9, 2011 at 9:53

3 Answers 3

1

Since your class is in the package helloworld you should run it like this:

java helloworld.HelloWorld

Also make sure "." is on your classpath.

Sign up to request clarification or add additional context in comments.

1 Comment

It worked. I had to typed it in the directory before the helloworld directory.
1

I try to compile it with the command: java HelloWorld

TO compile a java program you should use javac command like

javac Helloworld.java

6 Comments

I meant run the file not compiled it as it is already compiled.
@Victor: Are you providing all required jars in classpath when running it?
I don't have jars file only the HelloWorld.class and the HelloWorld.java
@Vector: if your class have package declaration then you should set your class path upto the folder containing helloworld folder instead of helloworld folder.
And how am I supposed to do that?
|
1

Are you sure that the directory where your classes are is in the classpath? Typically, in your project directory, the "classes" or "lib" directory.

If you are running from that directory, you could try adding ".".

See the -cp parameter of java runtime executable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.