8

I am very new to java and tried to run a simple code of calculating volume. The code is below:

package chapter6;

class Box {

    double width;
    double height;
    double depth;
}

package chapter6;

    public class BoxDemo {

        public static void main(String[] args) {

            Box myBox = new Box();
            double vol;

            myBox.depth = 1;
            myBox.height = 2;
            myBox.width = 3;

            vol = myBox.depth * myBox.height * myBox.width ;        

            System.out.println("Volume: " + vol);
        }

    }

I am able to run the code from eclipse, but when i try to run the code in Command Prompt i get the error:

C:\Prabhjot\Java\CompleteRefence\build\classes>java BoxDemo.class
Exception in thread "main" java.lang.NoClassDefFoundError: BoxDemo/class
Caused by: java.lang.ClassNotFoundException: BoxDemo.class
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: BoxDemo.class.  Program will exit.

Please assist.

5 Answers 5

14

First class file should be at this location:

C:\Prabhjot\Java\CompleteRefence\build\classes\chapter6\BoxDemo.class

Then you should be inside:

C:\Prabhjot\Java\CompleteRefence\build\classes>

Then issue the command:

java chapter6.BoxDemo

enter image description here

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

Comments

4

You have put your class in a package called chapter6. This means that the java file should be in a folder called chapter6 in the class root folder of your project. And when you run it, you should be in the root folder and use the command java chapter6.BoxDemo

1 Comment

thanks, now i got it. I was doing a mistake of using the command from C:\Prabhjot\Java\CompleteRefence\build\classes\chapter6 which shaould have been the way u mentioned. thanks!!
2

There is mistake in how you are running your program from console.

You are doing

java BoxDemo.class

But you need to do only

java BoxDemo

While running your program you don't need to mention .class with the name.

and if you are accessing it from root folder then you need to do

java chapter6.BoxDemo

Comments

1

try this

C:\Prabhjot\Java\CompleteRefence\build\classes>java chapter6.BoxDemo (RUN)

There is no need to specify .class extinction to the file while running.After compiling the java file it will create the .class file.

EXAMPLE

3 Comments

the problem above is my code is in package, pls see: package chapter6;
No need for the -d in this example, and your package name is "wrong", but other than that, it's correct now.
Great! I'm not able to undo the vote though, because both my comments crossed your edits...
0

When you invoke BoxDemo.class, Java looks for a class called class in the BoxDemo package, which doesn't exist. As you can see from the output java.lang.NoClassDefFoundError: BoxDemo/class, it's searching for a directory BoxDemo.

Instead, just specify the class name: BoxDemo; e.g. java BoxDemo.

2 Comments

I'm not sure why this was downvoted, as this provides the exact same answer as above, just with more info.
I didn't downvote, but your example won't work, since the package name is not correct.

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.