Monty
Monty

Reputation: 13

How to compile a package in Java from command prompt in Windows 7 system

I am learning Java from ‘Java2: The Complete Reference by Schield”. I am using a Windows 7 system with command prompt (JDK) to compile and execute Java programs. I am trying to compile a program having a package as seen in the code below:

import java.awt.*;
package MyPack;
class Balance
{
    String name;
    double bal;
    Balance(String n, double b)
    {
        name = n;
        bal = b;
    }
    void show()
    {
        if(bal<0)
        {
            System.out.print("--> ");
            System.out.println(name + ": $" + bal);
        }   
    }
}
class Account
{
    public static void main(String args[])
    {
        Balance current[] = new Balance[3];
        current[0] = new Balance("K. J. Fielding", 123.23);
        current[1] = new Balance("Will Tell", 157.02);
        current[2] = new Balance("Tom Jackson", -12.33);
        for(int i = 0; i < 3; i++) current[i].show();
    }
}

When I execute either

C:\Program Files\Java\jdk1.7.0_25\bin\javac" Account.java” (from within the MyPack folder

or

C:\Program Files\Java\jdk1.7.0_25\bin\javac" MyPack/Account.java (outside the MyPack folder)

I receive the following error:

“ Account.java:2: error: class, interface, or enum expected package MyPack;”

or

“MyPack\Account.java:2: error: class, interface, or enum expected package MyPack;”

Please let me know how I can compile and then execute such a program?

Upvotes: 0

Views: 60

Answers (1)

Stultuske
Stultuske

Reputation: 9437

To quote: https://docs.oracle.com/javase/tutorial/java/package/createpkgs.html

The package statement (for example, package graphics;) must be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

Upvotes: 2

Related Questions