943,880 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1206
  • Java RSS
Jul 4th, 2009
0

What is the function of "-d ." command?

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  1. package MyPack;
  2.  
  3. /**
  4.  *
  5.  * @author Shashank Sawant
  6.  */
  7. class Balance {
  8. String name;
  9. double bal;
  10.  
  11. Balance(String n, double b){
  12. name = n;
  13. bal = b;
  14. }
  15. void show(){
  16. if(bal<0)
  17. System.out.print("->");
  18. System.out.println(name + ":$" + bal);
  19. }
  20. }
  21.  
  22. class AccountBalance {
  23. public static void main (String args[]){
  24. Balance current[]=new Balance[3];
  25.  
  26. current[0]=new Balance("K. J. Fielding",123.23);
  27. current[1]=new Balance("Will Tell",157.02);
  28. current[2]=new Balance("Tom Jackson",-12.23);
  29.  
  30. for(int i=0;i<3;i++)
  31. current[i].show();
  32. }
  33. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sgsawant is offline Offline
9 posts
since May 2009
Jul 5th, 2009
0

Re: What is the function of "-d ." command?

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:

Java Syntax (Toggle Plain Text)
  1. >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 Syntax (Toggle Plain Text)
  1. >java MyPack.AccountBalance

If you want to store compiled classes in another folder other that current directory.
Java Syntax (Toggle Plain Text)
  1. >javac AccountBalance.java -d c:\test
Last edited by adatapost; Jul 5th, 2009 at 1:59 am.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 5th, 2009
0

Re: What is the function of "-d ." command?

Click to Expand / Collapse  Quote originally posted by adatapost ...
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:

Java Syntax (Toggle Plain Text)
  1. >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 Syntax (Toggle Plain Text)
  1. >java MyPack.AccountBalance

If you want to store compiled classes in another folder other that current directory.
Java Syntax (Toggle Plain Text)
  1. >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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sgsawant is offline Offline
9 posts
since May 2009
Jul 5th, 2009
0

Re: What is the function of "-d ." command?

Thanks,

I have correct that.

See,
Java Syntax (Toggle Plain Text)
  1. 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
Java Syntax (Toggle Plain Text)
  1. c:\xyz>java -cp c:\pqr MyPack.AccountBalance

or
Java Syntax (Toggle Plain Text)
  1. c:\pqr>java MyPack.AccountBalance
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 6th, 2009
0

Re: What is the function of "-d ." command?

c:\jtemp\adat>java MyPack.AccountBalance
should work. It works for me.
Reputation Points: 10
Solved Threads: 4
Light Poster
ThinkFree is offline Offline
26 posts
since Jul 2009
Jul 7th, 2009
-1

Re: What is the function of "-d ." command?

Click to Expand / Collapse  Quote originally posted by sgsawant ...
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:
Java Syntax (Toggle Plain Text)
  1. package MyPack;
  2.  
  3. /**
  4.  *
  5.  * @author Shashank Sawant
  6.  */
  7. class Balance {
  8. String name;
  9. double bal;
  10.  
  11. Balance(String n, double b){
  12. name = n;
  13. bal = b;
  14. }
  15. void show(){
  16. if(bal<0)
  17. System.out.print("->");
  18. System.out.println(name + ":$" + bal);
  19. }
  20. }
  21.  
  22. class AccountBalance {
  23. public static void main (String args[]){
  24. Balance current[]=new Balance[3];
  25.  
  26. current[0]=new Balance("K. J. Fielding",123.23);
  27. current[1]=new Balance("Will Tell",157.02);
  28. current[2]=new Balance("Tom Jackson",-12.23);
  29.  
  30. for(int i=0;i<3;i++)
  31. current[i].show();
  32. }
  33. }
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
Reputation Points: 8
Solved Threads: 15
Light Poster
sincerelibran is offline Offline
41 posts
since Mar 2009
Jul 7th, 2009
0

Re: What is the function of "-d ." command?

sincerelibran,
-d for dead
-d for drop
-d for duplicate
Do not write anything.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 7th, 2009
0

Re: What is the function of "-d ." command?

Click to Expand / Collapse  Quote originally posted by adatapost ...
Thanks,

I have correct that.

See,
Java Syntax (Toggle Plain Text)
  1. 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
Java Syntax (Toggle Plain Text)
  1. c:\xyz>java -cp c:\pqr MyPack.AccountBalance

or
Java Syntax (Toggle Plain Text)
  1. 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
Attached Thumbnails
Click image for larger version

Name:	java_prob_daniweb.JPG
Views:	22
Size:	154.8 KB
ID:	10775  
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sgsawant is offline Offline
9 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Trouble with area
Next Thread in Java Forum Timeline: Question of application implementation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC