2

I have two classes, Parent and Child. The code for the classes are like this:

Parent.class

package test;
import java.util.*;

public class Parent {
    public static void main(String[] args) {
        Child child = new Child();
    }
}

Child.class

package test;
import java.util.*;

public class Child {
    public Child() {
        System.out.println("A Child object has been created");
    }
}

I put both classes in a directory named "test". I can compile Child.java without any problem but I cant Compile Parent Class. It says that it cannot find the child class. What is the problem?

assylias
  • 321,522
  • 82
  • 660
  • 783
flav
  • 175
  • 1
  • 2
  • 6
  • 8
    Where are you compiling these _from_? Can you provide the command line? These look good to me as they are. – Louis Wasserman May 30 '12 at 08:46
  • See http://stackoverflow.com/questions/4800781/how-to-compile-multiple-java-source-files-in-command-line – Andrei Fierbinteanu May 30 '12 at 08:53
  • CLASSPATH....famous classpath...make sure that folder containing test is in classpath – ejb_guy May 30 '12 at 08:53
  • @LouisWasserman: I open the command prompt and go to the directory which .java files reside, just to clarify the name of that directory is the same as the package name that I used in the .java files. when I type in "javac parent.java" I'll get the error message. I also tried to type in "javac test\parent.java" and I could compile it but I got the error message when I typed in "java Parent". Im confused – flav May 30 '12 at 18:30

3 Answers3

0

I guess you execute the command in a error place. If you put the code in d:/test/, you should execute your command under the directory d:/.

So, if you change your directory to d:, you can do the right things. javac test\Child.java javac test\Parent.java

java test.Parent

You will get what you want..

linuxlsx
  • 138
  • 10
  • now it compiles both classes without any problem but when I try "java test\Parent" wrong name: test/Parent!! – flav May 30 '12 at 09:03
  • it shoud be "java test.Parent" . test.Parent is the name of your class Parent. – linuxlsx May 30 '12 at 09:08
  • Thank you I could compile it and run it using the instruction you gave me. now the problem is how should I setup "PATH" so i wouldnt need to type in the package name everytime that i want to run the file? – flav May 30 '12 at 19:37
  • I think you make a mistake. The full qualified name of a java class is "package_name.class_name" . So you can't execute "javac" cammand without specified the full name of the class. – linuxlsx May 31 '12 at 13:03
  • So you can't execute "javac" cammand without specified the full name of the class. At the same time, i think your "PATH" means "CLASSPAHT". You can get more informations about it by reading books or just google it. – linuxlsx May 31 '12 at 13:08
0

The thing to do is to include your generated child class in class path while you compile parent class.

SET CLASSPATH= (the path where your child.class exists);%CLASSPATH%;

The above needs to be done because the parent class is dependent on the child class and cannot be compiled without it

some details can be found here:

http://docs.oracle.com/javase/tutorial/getStarted/problems/index.html

MozenRath
  • 9,652
  • 13
  • 61
  • 104
  • could you please be more specific on how to setup PATH?? – flav May 30 '12 at 19:44
  • dont tamper with PATH just use CLASSPATH. `SET CLASSPATH=(the path where your child.class exists);%CLASSPATH%;` – MozenRath May 30 '12 at 21:39
  • while you try to learn these basics keep in mind that multiple classes are usually compiled using a build tool like `Ant` or `Maven` – MozenRath May 30 '12 at 21:45
0

Assume that your java files are in D:\test. And you compile likes this

     D:\>javac test/Child.java
     D:\>javac test/Parent.java

It will be OK for you.If you use some IDEs, your classes have no problem.

Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65
  • I know that IDE would do it for me easily but I want to learn the fundamentals of it. thanks for you answer. – flav May 30 '12 at 19:45