Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javac and compiled class file

 
Larsen Raja
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Where would the compiled class file be usually geenrated?

As per K & B,

By default, the compiler puts a .class file in the same directory as the .java source file.


Consider the below case:

[img]
myProject
|
|--source
| |
| |-- MyClass.java
|
|-- classes
|
|--[/img]

And in this case, if I compile from MyProject directory where would the class be generated?

myProject> javac source/MyClass.java

Larsen.
 
Alex Theedom
Author
Ranch Hand
Posts: 77
5
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you compile from the myProjects directory and you want the class file to be compiled into the sources directory use should use the following command:

javac source/MyClass.java

However if you want the code to be compiled into a specific directory (for example the classes directory) you must specify it with the -d option tag like so:

javac -d classes source/MyClass.java

As you can see the K&B book is correct:

By default, the compiler puts a .class file in the same directory as the .java source file.
 
Larsen Raja
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't javac source/MyClass.java be creating class file in myProjects directory?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Larsen Raja wrote:Shouldn't javac source/MyClass.java be creating class file in myProjects directory?

By default, the compiler puts a .class file in the same directory as the .java source file.
 
Larsen Raja
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I conclude that always class files will be created in the source folder of .java file when compiled without -d option and from any directory?
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac - The Java Compiler says -
Unless you specify the -d option, the compiler places each class file in the same directory as the corresponding source file.


Regards,
Dan
 
Larsen Raja
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic