2

I'm using Eclipse and I have two different projects: A and B.

In project A, I have a class classA where I need to call a method methodB() from a class classB contained in the project B, how can I do that?

I've tried adding the project B to project A build path, but still doesn't work.

Thanks.

Ankit Popli
  • 2,809
  • 3
  • 37
  • 61
user3333069
  • 61
  • 1
  • 1
  • 3
  • Forgot to say that methodB() is static, so I don't need to create an instance of the class. – user3333069 Feb 20 '14 at 14:23
  • First make sure that in `project B`, `classB` compiles properly – Sajan Chandran Feb 20 '14 at 14:23
  • This question already answered, see here: http://stackoverflow.com/questions/1106023/how-to-use-classes-from-another-project – Maxim Kirilov Feb 20 '14 at 14:24
  • classB is compiling properly, I can call methodB() from classB main() and it works – user3333069 Feb 20 '14 at 14:25
  • What is the access scope for Class B & methodB() ? Does it work on public scope ? Some times Eclipse behave strangely and does not resolve the dependencies automatically. You could try Refresh the projects / Project > Clean. In some strange cases a eclipse restart may be needed...rarely. – Jay Feb 20 '14 at 14:28
  • What is the error you get? What are the names of the projects? Are the classes you're dealing with in packages, and if so what are then names of these packages? What are the names of the classes? Do you have an import statement and if so, what is it? – George Tomlinson Feb 20 '14 at 16:18

4 Answers4

4

You need to add another project in "Project" tab, or add class folder of the project in "Libraries" tab ie you may try to add project B to the Run configuration used by project A. Go to the menu Run -> Run configurations, ther you can add the project B in the tab 'classpath' of your run configuration.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

Here's an example that you may find helpful:

Project_1 has the following class:

ClassProjectOne.java which consists of:

public class ClassProjectOne {

    private int m_Age;
    private final int AGE_INPUT = 15;

    public ClassProjectOne() {
        setAge(AGE_INPUT);
    }

    public int getAge() {
        return m_Age;
    }

    private void setAge(int age) {
        m_Age = age;
    }
}

Project_2 has the following class:

ClassProjectTwo.java which consists of:

public class ClassProjectTwo {

    public static void main(String[] args) {
        ClassProjectOne t = new ClassProjectOne();
        System.out.println(t.getAge());
    }

}

In order for this to work, you must right click Project_2 and click on Properties. Then click on Java Build Path -> Add... -> Select Project_1 -> OK. This sets a Java Build Path.

If your class is static there is no need to initialize a new instance of it.

Hope this helps.

Rhys
  • 2,055
  • 2
  • 18
  • 23
0

I've just done what you're trying to do. I called my first project 'project1'. In this projects i have a package called 'package1' which in turn contains a class called 'Class1' containing a (public) static method called 'staticMethod'. I called my second project 'project2' with a class 'Class2' in 'package2'. I added project1 to the build path of project2 and then inserted the statement import package1.Class1 at the beginning of the class Class2.

George Tomlinson
  • 1,871
  • 3
  • 17
  • 32
-1

Put the Project B on the Build path, then do a Clean project from Project Menu option and then use it.

Click in "A" --> Properties --> Build Path --> Projects ---> Add the Project ---> Ok

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109