The program is given at the bottom
To compile java programs in the command prompt I use the following command:
C:\jtemp\MyPack>javac AccountBalance.java -d .
Only then are the classes compiled and I am able to run the main() function. When I execute the above command a folder "MyPack" is created inside the pre-existing MyPack folder. It contains the 2 classes specified in the program.
Then to execute the compiled program I use the following command:
C:\jtemp\MyPack>java MyPack.AccountBalance
No doubt the program succeeds and prints the expected lines.
Now that's fair enough. The reason I am writing this thread is that I am unable to understand the true significance of the "-d ." command. If I try to compile the classes without it, firstly no folder is created inside the pre-existing folder "MyPack" and secondly the following commands yield a "NoClassDefFoundError". Please note that the classes are still compiled and appear just besides the .java file in the window. It is just that they do not execute successfully this time.
C:\jtemp\MyPack>java MyPack.AccountBalance
C:\jtemp\MyPack>java AccountBalance
I know it has got something to do with the "Classpath" variable but even after reading about it, it's still Greek and Latin to me.
Please help. The Program is stored at the address "C:\jtemp\MyPack>" on my hard-disk, has the name "AccountBalance.java" and is as follows:
package MyPack;
/**
*
* @author Shashank Sawant
*/
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 AccountBalance {
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.23);
for(int i=0;i<3;i++)
current[i].show();
}
}