0

I checked few other stackoverflow posts. But I am not able to call a function that is present in another project.

SampleTwo.java

package a.two;

public class SampleTwo {
    public static void bar() {
        System.out.println("Bar");
    }

    public static void main(String[] args) {
        bar();
    }
}

Updated SampleOne.java

package a.samp;
import a.two.*;

public class SampleOne {
    public static void foo() {
        System.out.println("Foo");
        SampleTwo.bar();  // <------ I want this to work
    }

    public static void main(String[] args) {
        foo();
    }
}

Here is my project properties of both projects [Version 1: Before blackjack26's answer] Eclipse shows the error SampleTwo cannot be resolved Can you please tell if I am missing something? Thanks!

[Version 2: After blackjack26's answer]

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    SampleTwo cannot be resolved

[Version 3] Removed SampleOne from SampleTwo's project property fixed it.

Community
  • 1
  • 1
Anit
  • 345
  • 1
  • 5
  • 19
  • You should import the project two reference – Fady Saad Apr 08 '17 at 03:16
  • if you include the project into your class path, and properly import the class you will have no problem. Please share your error message. – Yohannes Gebremariam Apr 08 '17 at 03:17
  • You need to tell Eclipse to link the 2nd project into the 1st one. – Hovercraft Full Of Eels Apr 08 '17 at 03:18
  • @Yohannes I get "SampleTwo cannot be resolved" on Eclipse – Anit Apr 08 '17 at 03:20
  • 1
    Did you import the class ? I didn't see `import a.two.SampleTwo; ` in your SampleOne.java. Otherwise, Eclipse is pretty much helpful if you know how to properly use the tool. hover over the error message and it will tell you possible options you can do to fix your issue. – Yohannes Gebremariam Apr 08 '17 at 03:23
  • Technicaly, it is illegal to do a circular dependency as in your case. You can only have one project dependent on the second project, or the other way, but not both way. I am not sure if this is the culprit though. Eitherway it seems you don't have syntax issue, fix your dependencies – Yohannes Gebremariam Apr 08 '17 at 03:37

1 Answers1

1

In your SampleOne Java file, you are missing and import to access SampleTwo.

You should add the following to your imports for SampleOne:

import a.two.SampleTwo;
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
bnjc
  • 56
  • 2