By shaw

2011-03-21 10:25:14 8 Comments

I have a Java program called, it is located in the following directory:


When I try to run from the 'program' directory, everything goes ok, I use this line:

/home/user/program$ java Main

But when I try to run from the home directory :

/home$ java /home/user/program/Main

I get :

Exception in thread "main" java.lang.NoClassDefFoundError: /home/user/program/Main
Caused by: java.lang.ClassNotFoundException: .home.user.program.Main

What is the cause of this error?


@sugavaneshb 2013-09-13 08:51:05

I came across this same error when trying to compile and run it. The book, "Head First Java" explains and addresses this problem appropriately. Here is a screenshot from the book for your reference.

A screenshot from Head First Java about how to compile and run a java program by including package names.

Hope its helpful.

@Jonah Tornovsky 2013-07-06 20:02:31

just a while ago faced this kind of error of (NoClassDefFoundError). I imported some third party library in my android app using eclipse env. I got this error during a runtime - some class from this third party library couldn't be found and a result of this NoClassDefFoundError was thrown, despite the mentioned library correctly appeared in classpath, so I really didn't know what else can be done to solve this problem. While playing with "Order and Export" tab within "Java Build Path", I put my imported third party library to the top of the list of all libraries in my project and checked its checkbox - this solved the problem

@Michael Laffargue 2011-03-21 10:28:36

Because its looking for the class using the fullname you give (/home/user/program/Main). You should only look for the Main class but using the good classpath : java Main -cp /home/user/program

Which means it'll search the Main class in the given set of paths

@Mike Tunnicliffe 2011-03-21 10:30:39

This is due to your classpath, which will default to the current directory. When you run java Main from /home/user/program it finds the class in the current directory (since the package seems to be unset, meaning it is the default). Hence, it finds the class in /home/user/program/Main.class.

Running java /home/user/program/Main from /home tries to find the class in the classpath (the current directory) which will look in /home/home/user/program expecting to find the file Main.class containing a definition of the Main class with package .home.user.program.

Extra detail: I think the java launcher is trying to be nice by converting /-notation for a classname to the .-notation; and when you run java /home/user/program/Main it is actually running java .home.user.program.Main for you. This is because you shouldn't be specifying a file, but a fully specified classname (ie including package specifier). And when a class has a package java expects to find that class within a directory structure that matches the package name, inside a directory (or jar) in the classpath; hence, it will try to look in /home/home/user/program for the class file

You can fix it by specifying your classpath with -cp or -classpath:

java -cp /home/user/program Main

@Eric Leschinski 2013-12-28 00:19:19

If you have a package defined you may need to do this: java -cp /home/user/program:. Main The extra :. means "and the current directory.

@dgorissen 2011-03-21 10:35:07

This works for me:

java -cp /home/user/program Main

@Thomas 2011-03-21 10:34:06

The problem is that if you call java /home/user/program/Main the package Main is in is meant to be home.user.program, which I assume is not true for Main (I assume it's in the default package, i.e. none at all). Is there a package declaration at the top of Main?

I'd suggest to use the classpath suggestions in the other answers.

@helios 2011-03-21 10:31:38

Because of what you say I conclude this:

  • Main is in "top" (root) package

And when you execute java you must indicate the classpath, it is, the root directory where your pakage and classes structure is located.

In your case it is the very /home/user/program. And I guess your classpath is defined as "." (the dir you are located at). When you call java from home the classpath is being taken erroneosly.

If you want to call your main using a different package declare the package at the top of the class:

package user.program;

And set the classpath to /home (or execute java from that dir). Next call java this way:

java user.program.Main

using dots because its a full class name (indicating packages). That is translated to dirs concatenating classpath + package + class. By example:

user.program -> user/program/
Main         -> Main.class

Good luck!

@shaw 2011-03-21 10:42:19

thanks for the info, this made me understand more what does a classpath mean

@Heiko Rupp 2011-03-21 10:29:47

Your 2nd command version does not know where to find the classes. You need to provide the so called classpath

/home$ java -cp userprogram Main

@helios 2011-03-21 10:32:39

-cp user/program I guess :)

@Heiko Rupp 2011-03-21 10:34:15

Right - I saw /home/user as the home directory

Related Questions

Sponsored Content

45 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

38 Answered Questions

[SOLVED] Can't execute jar- file: "no main manifest attribute"

87 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

33 Answered Questions

[SOLVED] When to use LinkedList over ArrayList in Java?

58 Answered Questions

[SOLVED] How to create a memory leak in Java?

66 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 4094905 View
  • 3530 Score
  • 66 Answer
  • Tags:   java random integer

44 Answered Questions

[SOLVED] Can't start Eclipse - Java was started but returned exit code=13

51 Answered Questions

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

1 Answered Questions

[SOLVED] Why does array[idx++]+="a" increase idx once in Java 8 but twice in Java 9 and 10?

Sponsored Content