Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

Here is my code:

       package MyPack
 

       class Balance

       {

        String name;

        double bal;

        Balance(String n, double b)

        {

            name=n;

                    bal=b;

            }

        void show()

            {

            if(bal<0)

            System.out.println("-->");

            System.out.println(name+ ": $:" +bal);

        }

        }

        class AccountBalance

        {

            public static void main(String args[])

            {

                       Balance current[]=new Balance[3];

                    current[0]=new Balance("A.K.Juwatkar",123.123);

                    current[1]=new Balance("A.P.Dhoke",345.67);

                    current[2]=new Balance("Anil Sarang",100.98);

                    for(int i=0;i<3;i++)

                current[i].show();

               }

        }

I am using Ubuntu 10.04 and when I compile it using

Java MyPack.AccountBalance

I get the following message:

Exception in thread "main" java.lang.NoClassDefFoundError: MyPack/AccountBalance

Caused by: java.lang.ClassNotFoundException: MyPack.AccountBalance

    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: MyPack.AccountBalance. Program will exit.

What is wrong? Please help me out.

1 Answer

0 votes
by (13.1k points)

You have to compile and run the classes from outside the packages:

First, you compile with javac:

$javac MyPack/AccountBalance.java

This will create a new file in the MyPack folder called AccountBalance.class

Then you can run it:

$java MyPack.AccountBalance

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 17, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Oct 9, 2019 in Java by Anvi (10.2k points)

Browse Categories

...