27

I have written a simple package program:


  //A simple package

       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 & 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. I have installed openjdk, do i need to install anything else?? I am using Ubuntu 10.04, kindly help me out

user1388626
  • 273
  • 1
  • 3
  • 5

5 Answers5

41

Best is 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

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

Grims
  • 777
  • 5
  • 6
18

When you are trying to compile the java class, use the '-d' option (destination) to specify where the .class files should be located.

javac -d "classes" AccountBalance.java

and when you run your program, make sure that same folder is included in your class path:

java -classpath "classes" MyPack.AccountBalance
bool.dev
  • 17,508
  • 5
  • 69
  • 93
shayan ray
  • 321
  • 1
  • 3
6

Try to create the folder named MyPack and move .class files in it or use following command line to compile the java code and it moves the .class files into MyPack folder.

javac YourFileName.java -d .

and run using,

java MyPack.AccountBalance

Have a look at javac tool

From the tool doc : By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with -d (see Options, below).

and package tutorial.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • I compiled by javac AccountBalance.java got no errors there But when i tried to run using java MyPack.AccountBalance i got the following msg: – user1388626 May 11 '12 at 07:04
  • @user1388626 You need to create MyPack folder and move Balance.class and AccountBalance.class in it. Or use -d switch so the javac tool creates the package folder. – KV Prajapati May 11 '12 at 07:09
  • I have placed AccountBalance.java file in MyPack folder – user1388626 May 11 '12 at 07:11
  • @user1388626 then come out from this folder (back to parent of MyPack) and run the >java MyPack.AccountBalance – KV Prajapati May 11 '12 at 07:12
3

If you're frequently compiling and running via javac and java commands then use

javac MyJavaClass.java && java MyJavaClass

This'll compile the the class then run the class you just compiled.

Note: Replace && with ; if using Windows powershell.

James T.
  • 910
  • 1
  • 11
  • 24
  • with `java` command you don't need to specify the `.class` extension, but only the fully-qualified class name of the class containing the `main` method – user2340612 Jun 16 '17 at 21:37
  • Windows uses this syntax too. – Nahiyan Jul 29 '17 at 09:00
  • 1
    @Nahiyan Windows cmd uses this syntax, but Windows powershell uses `;` to run one command after another. Updated my answer again. – James T. Jul 30 '17 at 18:34
0

I think you misunderstand java command. If you want to compile a java code like MyClass.java .

package myPackage
public class MyClass{
  ...
}

compile the package

javac -d . MyClass.java

which will create /myPackage/MyClass.class in "."(current directory).

-d means destination directory.

Then run it.

java myPackage.MyClass
Yanshu Du
  • 1
  • 2