Sunday 4 December 2011

Referring to another project using Eclipse

There are two seperate projects. Main project calls a method in the ReferredProject.


ReferredProject has
referredProjectPackage/ReferenceClass.java
package referredProjectPackage;

public class ReferenceClass {
public void sayHello(){
System.out.println("Hello from the reference class.");
}
}
MainProject has
mainPackage/MainClass.java
package mainPackage;
import referredProjectPackage.*;

public class MainClass {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("MainClass works.");
ReferenceClass rc = new ReferenceClass();
rc.sayHello();
}

}
It gives error:



Right Click on the MainProject in the PackageExplorer. Select properties at the bottom. Properties for the MainProject window opens. Select JAVA build path and then select the Projects tab.



Select ReferredProject and then click OK. The errors disappear and the program works.
But to be on the safe side, do not forget to do also:
Right click on the MainProject, select Project References, select ReferredProject and click OK.