aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes javac and compiled class file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "javac and compiled class file" Watch "javac and compiled class file" New topic
Author

javac and compiled class file

Larsen Raja
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
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
Ranch Hand

Joined: Jan 18, 2012
Posts: 48

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.


Alex Theedom - Senior Java Developer (OCPJP/OCPJWCD)

Co-author of Professional Java EE Design Patterns: http://www.amazon.co.uk/-/e/B00J7LBJZS
Larsen Raja
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Shouldn't javac source/MyClass.java be creating class file in myProjects directory?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
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.


Joanne
Larsen Raja
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
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

Joined: Jul 09, 2001
Posts: 1180
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


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Larsen Raja
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javac and compiled class file