Hi Mrinal

When you are in command line (in a DOS window for e.g.) the "javac" is able to compile any file in any directory you wish, as long as you properly specify the absolute or relative directory, according to the DOS rules.

For e.g. the file D:\foo\bar\pack\Point.java compiles :
-- either with "javac D:\foo\bar\pack\Point.java compiles"
-- or with "javac Point.java" if the current directory is "D:\foo\bar\pack\"
-- or with "javac pack/Point.java" if the current directory is "D:\foo\bar\"

However, if your .java file contains references to other classes, then javac tries to resolve dependencies according to the Java rules for classes and packages.

By default, javac considers that the "CLASSPATH" is the current directory (i.e. ".").

If "Line.java" is in "D:\foo\bar\shapes\" and extends "Point" (that is not in a package), you might wish to make the current directory "D:\foo\bar\pack\" (cd D:\foo\bar\pack) and compile Line.java using "javac D:\foo\bar\shapes\Line.java". In this way, the javac compiler finds the file to compile by the full name and the referenced class "Point" as the file Point.java or Point.class can be found in the current directory.

If the class "Point" is in a package and is referenced as "pack.Point". Then you migh like to "cd D:\foo\bar", then "javac D:\foo\bar\shapes\Line.java". In this case the javac compile searches for a "pack.Point" starting from the current directory (i.e. looks for a sub-directory "pack" and a file "Point.java" ou "Point.classé in this sub-directory).

You can also play with the CLASSPATH system variable or with "-cp" command line option, but you will get lost easily if you don't respect some simple discipline:
1./ Make a root directory for your project, such as "D:\foo\bar\prj1".
2./ Put all your .java file in sub-directories corresponding to the package name. If "Point" is in the package "pack1" and "Line" is the package "pack2", then you must have the files "D:\foo\bar\prj1\pack1\Point.java" and "D:\foo\bar\prj1\pack2\Line.java". 3./ For compiling purposes, make the current directory to be your project root: "cd D:\foo\bar\prj1". 4./ Compile using the package name as relative directory name: "javac pack1/Point.java" and "javac pack2/Line.java".

And everything becomes simple!

Unless you wish to do it more sophisticated!

The compiler put the .class files in the same directory as the original .java files unless you use the -d option to tell him where to put the files. It is a good practice to separate the sources from the compiled classes (you might wish to deliver to the customer only the compiled code, not the sources and it would be easier to separate the file if they are in different directories from the beginning).

In this case:
1./ Your project root directory might have two sub-directories, one for sources, one for classes: "D:\foo\bar\prj1\src" and "D:\foo\bar\prj1\cls" (for e.g.). 2./ You would have your source files in sub-sub-directories corresponding to the package names: "D:\foo\bar\prj1\src\pack1\Point.java" and "D:\foo\bar\prj1\src\pack2\Line.java". 3./ Make the current directory to be the project root "cd D:\foo\bar\prj1" (just like above - no change at this step). 4./ Compile using the "-d" option (specifying the destination directory for the ".class" files) and the "-cp" option (specifying where to start looking for referenced files - pack1.Point can no longer by found starting from the current directory) :
    javac -cp ./src;./cls -d ./cls src/pack1/Point.java
    javac -cp ./src;./cls -d ./cls src/pack2/Line.java

As you can notice:
-- Of course, you must insert the "src/" before the name of the package (so that the relative file name to be properly constructed). -- The compiler will automatically create the "./cls/pack1" and "./cls/pack2" sub-sub-directories (from "./cls", as specified by the "-d" option). -- When compiling the "Line.java", the compiler have to look for "pack1.Point", i.e. for a sub-directory "pack2" (starting from a point in the classpath) containing a file Point.class our Point.java. As the parameter "-cp" gives a list of places to start looking from, javac will first look for some file "./cls/pack1/Point.class" or "./cls/pack1/Point.java", then for "./src/pack1/Point.class" or "./src/pack1/Point.java", whichever can be found first. -- Thus way of doing allows to compile file in any order (exactly as in the "simple" case).

It seems a little complicated, but:
-- It is like bycicle: once you have understood how it works you never forget. -- Advanced tools like NetBeans and Eclipse can do this automatically - you don't need to bother (although you can be sure to be able to do what a machine can do - programmers are smarter then their tools). -- Such a simple discipline (combined with the use of .jar files) allow to manage sources and binaries of huge applications (using several thusend of classes).

I have a train to catch, so I am in a sort of hurry. So I didn't test all my sayings. It is possible to have some errors, but if you understood the idea, debugging is a good exercice :-)

Hope it helps.
Mihai


On 06/09/2012 12:14, Mrinal Roy wrote:
Hi,
I have a Point.java file and Line.java file in package named "roymrinal_2dShape" package. Line class in line.java expends Point class; Point.java compiles with no error.
But Line.java produces error as bellow:
bad class file: D:\Working Data\Quellcode\roymrinal_2dShape\Point.class
class file contains wrong class: roymrinal_2dShape.Point
Please remove or make sure it appears in the correct subdirectory of the classpath.
public class Line extends Point {
Please guide me : what is the correct directory to place the 2 class code files?
Regards
Mrinal
--
You received this message because you are subscribed to the Google Groups "JPassion.com: Java Programming" group. To unsubscribe from this group, send email to [email protected].
Visit this group at http://groups.google.com/group/jpassion_java?hl=en-US.



--
You received this message because you are subscribed to the Google Groups 
"JPassion.com: Java Programming" group.
To unsubscribe from this group, send email to 
[email protected].
Visit this group at http://groups.google.com/group/jpassion_java?hl=en-US.


Reply via email to