Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compilation of java files using javac -d option

 
Jignesh Gohel
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
If i have the following structure :


In pack2 directories there are some classes which depends on some classes in pack3 directories and vice-versa.

Now if i want to compile the src files using the following command pattern:
javac -d classes src

how to do it..
i have tried but it gives compilation errors.

And can somebody please let me know how Eclipse does it when it automatically compiles the code we are writing???


Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49779
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is awkward; you have to start compiling the file which has no dependencies on any other files in your package structure. It is the one farthest from the main method, which can be difficult to guess.

The class with the main method in will be compiled last. Slightly easier to guess which one.

Don't know how the IDEs do it.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its not that bad, if a dependent class is on the source path the javac will find it and compile it. That said, I use Windows mostly and I usually just write a script called compile and call that from the command line, I keep all my class names listed in separate text file:

compile.bat


\resources\sourceclasses.txt

[ April 19, 2007: Message edited by: Garrett Rowe ]
 
Jignesh Gohel
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Garrett,


Its not that bad, if a dependent class is on the source path the javac will find it and compile it.


If you have any knowledge regarding how come this works please let me know i am very much curious to know about this.

Thanks
 
kiran kumar kondru
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can otherwise set the CLASSPATH to the correct directory i.e.

CLASSPATH=src;

probably your files in source are in good package structure.

How this works? Here it is

when you set the classpath to src, the javac or java uses them to find other classes. For this situation your file in pack2 will have the package statement as

package com.samples.java.pack2;

when this statement is encountered it checks whether a folder named "com" exists or not. for this it checks all the paths set in the classpath variable. since our classpath is set, it find com and it finds samples etc.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic