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();
    }
}

If your program file has package statement then all these classes defined inside the file must be stored into package name folder.

Example in above post:

>javac AccountBalance.java -d  .

statement creates a folder MyPack in current directory (. with command line) and both Balance.class and AccountBalance.class files are placed into MyPack folder.

Run - JVM launch AccountBalance class from file system folder MyPack.

>java MyPack.AccountBalance

If you want to store compiled classes in another folder other that current directory.

>javac  AccountBalance.java -d c:\test

If your program file has package statement then all these classes defined inside the file must be stored into package name folder.

Example in above post:

>javac AccountBalance.java -d  .

statement creates a folder MyPack in current directory (. with command line) and both Balance.class and AccountBalance.class files are placed into MyPack folder.

Run - JVM launch AccountBalance class from file system folder MyPack.

>java MyPack.AccountBalance

If you want to store compiled classes in another folder other that current directory.

>java  AccountBalance.java -d c:\test

Thanks for the reply. I am assuming that the last sentence in your post should have "javac" instead of "java". Please correct me if I am wrong.

I did that. Actually to be exact I did the following thing:
c:\jtemp\mypack>javac AccountBalance.java -d c:\jtemp\adat

As expected a folder "MyPack" was created inside the "adat folder.
For confirmation this is the address of the folder I am talking about:
c:\jtemp\adat\mypack>
Also, as per expectations, it has the 2 class files.

Now this is where my usual problem begins. The problem I can't get around no matter what I try; the problem who's answer is what I am seeking on the internet.

How should I execute these class files? Every attempt I make yields the result "NoClassDefFoundError".

Please help. These are the ways I have attempted to execute the classes:
1. c:\jtemp\adat\mypack>java MyPack.AccountBalance
2. c:\jtemp\adat\mypack>java .AccountBalance
3. c:\jtemp\adat>java MyPack.AccountBalance
4. c:\jtemp\adat>java AccountBalance

All result in the same error. It is not the compilation, or the place where the class files get stored is the issue. The issue is how do I run the class files? I know only one way - and I must thank you (adatapost) for that respite - but that method is too restricted. This only method is what I have given in my first post. But when I change the location where the classes are finally compiled, I can't run them.

Please help!

Regards,

-sgsawant

Thanks,

I have correct that.

See,

c:\xyz>javac AccountBalanace.java -d c:\pqr

.class files are copied at MyPack under c:\pqr folder.

Use -classpath or -cp swith with java launcher

c:\xyz>java -cp c:\pqr MyPack.AccountBalance

or

c:\pqr>java MyPack.AccountBalance

c:\jtemp\adat>java MyPack.AccountBalance
should work. It works for me.

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();
    }
}

Dear Friend,

-d stands for deprecated. That means, some of the functions/features which were used in the earlier versions of java and not included in the newer versions. You might be using some of those functionalities or features. That's why you're getting such an error. So when you compile you've to use -d. The syntax actually is javac -d Sample.java

commented: Wrong Information. -1

sincerelibran,
-d for dead
-d for drop
-d for duplicate
Do not write anything.

Thanks,

I have correct that.

See,

c:\xyz>javac AccountBalanace.java -d c:\pqr

.class files are copied at MyPack under c:\pqr folder.

Use -classpath or -cp swith with java launcher

c:\xyz>java -cp c:\pqr MyPack.AccountBalance

or

c:\pqr>java MyPack.AccountBalance

Thanks a lot!

The instruction to change the classpath works and executes the program. Even though after entering the directory where MyPack folder (containing the classes) is stored, the command
C:\jtemp\adat>java MyPack.AccountBalance
doesn't work. (refer the attachment)

Nevertheless my problem has been solved. Perhaps the classpath variable is locked by default and explicit instruction is necessary on my machine (just a guess).

Thanks to everyone, esp. adatapost!

Regards,

-sgsawant

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.