0

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

AVLNode.java
AVLTree.java
Comparator.java
HashTable.java
input1.dat
input2.dat
Main.java
StringComparator.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?

Community
  • 1
  • 1
SigmaOmega
  • 11
  • 1
  • 2

5 Answers5

1

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.

RP-
  • 5,699
  • 2
  • 25
  • 43
1

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

Radu Murzea
  • 10,036
  • 9
  • 44
  • 68
Subin Sebastian
  • 10,367
  • 3
  • 35
  • 41
1

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

clamp
  • 30,396
  • 73
  • 193
  • 291
ramroum89
  • 9
  • 2
0

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...

Radu Murzea
  • 10,036
  • 9
  • 44
  • 68
0

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
mebubo
  • 1