How to run Java Program with class having sub class in different package hierarchy?

I have two classes like:

Class GetData is like:

package com
public class GetData{
private String name = "John";
public String getName(){
    return name;
  }
}

package com.test;
import com.GetData;

public class Test{
   public static void main(String args[])
   {
       GetData data = new GetData();
       System.out.println("Welcome "+data.getName());
   }
}

When I'm trying compile the Test class it throws an error like undefined symbol for GetData. I tried like:

javac -cp .com/GetData.class Test.java

Kindly help me out

Jon Skeet
people
quotationmark

The problem is with how you're specifying the classpath. You just need to specify the directory which is at the root of the directory containing the classes, so if you've got com/GetData.class you just need:

javac -cp . Test.java

Although you should really have the source code in a directory hierarchy matching the packages, so I'd expect

javac -cp . com/test/Test.java

Then run with:

javac -cp . com.test.Test

All of this is assuming that . isn't on your classpath - it is by default, so you may well find you can just use:

javac com/test/Test.java
java com.test.Test

people

See more on this question at Stackoverflow