Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException when running in Command Prompt

Tags:

java

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.

like image 687
prabh Avatar asked Apr 27 '13 06:04

prabh


1 Answers

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

like image 137
Jops Avatar answered Sep 19 '22 23:09

Jops