2

I have been mostly using eclipse so far. Now I'm trying to run java from terminal but I have a problem with packages.

This is my Main.java file:

package main;

class Main {
    public static void main(String[] args) {
        System.out.println("it's working");
    }
}

I compile this using javac Main.java and then run with java Main which gives me:

java Main
Exception in thread "main" java.lang.NoClassDefFoundError: Main
Caused by: java.lang.ClassNotFoundException: Main
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Main. Program will exit.

When I remove package Main everything works fine. What am I missing?

java -version gives:

java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.4) (6b24-1.11.4-1ubuntu0.12.04.1)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
2
  • Your Java source file locations must (a) match their packaging, and (b) must be taken into account when setting the java command's classpath. Commented Oct 18, 2012 at 22:20
  • See my answer below, you need to be up in the root directory. Are you running this from within the 'main' directory? Commented Oct 18, 2012 at 22:21

6 Answers 6

6

You need to run the java command up one directory level and give it in the fully qualified package name, eg: java main.Main

See How the Java Launcher Finds User Classes to learn how this works.

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

Comments

0

You can use this command:

java main.Main

Make sure the main (lowercase) package directory is on the classpath.

Comments

0

It is possible that your classpath is not set correctly. Since you gave your .java file a package it is unnamed no longer.

An example:

java -cp ./package1/ main.Main //from the current directory and 
                               //if main package is contained in package1

You need to fully qualify the class name. For future reference if you want to run from the command line you must stop the indirection (for lack of a better term) at the package level. Say your class was in the package package1.package2.Main.java I would run it like so java -cp /blah/blah package1.package2.Main

Comments

0

Compile

Windows:
javac main\Main.java
Mac:
javac main/Main.java

Run

java main.Main

Comments

0

If you add package Main, then you must put your source file in folder Main/Main.java. After that you can compile. When you run the program, go to Main folder using "cd", then write java -cp Main.Main See my question similiar to yours noclassdeffounderror

Comments

0

try this...

In window , you just compile the code as

javac - d . Main.java

then a package(folder) with the name you specified in your class is created (in your code, package with name "main" is created) in the same path where your program reside...

Then you just run the program as java main.Main or java main/Main

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.