Take the 2-minute tour ×

I'm trying to run a Java app from the cmd, and I'm getting the following Errors:

Exception in thread "main" java.lang.NoClassDefFoundError: Main
    Caused by: java.lang.ClassNotFoundException: Main
    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)
    Could not find the main class: Main.  Program will exit.

In the dir you can find:

Directory of C:\Java


and I'm Running:

java Main input1.dat input2.dat  output1.dat

I have Main.Java in the folder and I have:

public static void main(String[] args) method on the Main.Java (and some more functions)

I already read the answers about this problem here but I think I did everything alright :( so what can be the problem?

share|improve this question
Try using a package next time. –  adarshr Jun 14 '12 at 9:14
Have you compiled java texts to classes? –  Alexei Kaigorodov Jun 14 '12 at 9:16
I do not see any .class files. This means that you probably did not compile your code. To compile a Java program, say p.java, do javac p.java –  Ankit Jun 19 '12 at 22:58

5 Answers 5

Check whether you have Main.class file in your current directory or not. If it is already there, check your path variable in you system environment variables. It should point to JAVA_HOME\bin.

share|improve this answer

as stated in another answer check if Main.class is present in current directory

also try java -cp . Main arg1 arg2

above line sets classpath to current directory

share|improve this answer

First make sure that you have compiled your code.

You should be in the directory that contains the Main.class (If you are using eclipse IDE it will be bin/).

If your Main class is in a package you should run your command from the directory that contains the package and your command will be java [package name].Main [arguments]

If you want to run your command from anywhere you can use the -cp option like the following :

java -cp [classpath] [package name].Main [arguments]

with classpath : path to the directory containing the .class until right before the package

share|improve this answer

In addition to what the others suggested, check to see if you have any package statements in Main. If so, you must include it when starting the program.

For example, if you have:

package mypackage;

public class Main
    public static void main (String[] affhf)


then you must start your program by calling:

java mypackage.Main input1.dat input2.dat output1.dat

This might slip by the compilation process in some situations...

share|improve this answer

Looks like you haven't compiled your code. Try the following:

cd Java
javac Main.java

This should create the .class files in the Java directory. Then, without changing the directory, try:

java Main input1.dat input2.dat output1.dat
share|improve this answer

Your Answer


