user1388626 user1388626 - 21 days ago 4x
Java Question

How to compile packages in java?

I have written a simple package program:

//A simple package

package MyPack

class Balance
String name;
double bal;

Balance(String n, double b)

void show()
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++)

I am using Ubuntu 10.04 &
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 Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
Could not find the main class: MyPack.AccountBalance. Program will exit.

What is wrong? Please help me out.
I have installed openjdk, do i need to install anything else??
I am using Ubuntu 10.04, kindly help me out


Best is to compile and run the classes from outside the packages :

First you compile with javac :

$javac MyPack/

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

then you can run it :

$java MyPack.AccountBalance

By the way: it is discouraged to have package names start with a capital.