Heinrich Schmetterling
Heinrich Schmetterling

Reputation: 6894

java "cannot find symbol" - how to import a file in the same directory?

I have two java files in a directory, neither of them are in a package. I want one of them to be able to reference the other. What is the right way to do this?

Currently in Class A, I'm trying to reference class B, and getting this error:

[javac] A.java:11: cannot find symbol
[javac] symbol  : constructor B(java.lang.String)
[javac] location: class B
[javac]         B b = new B(path);
[javac]               ^

Nothing below worked:

Thanks!

Upvotes: 0

Views: 5926

Answers (3)

TooManyLanguages
TooManyLanguages

Reputation: 41

Also, if you have a package reference in each file, you need to compile it from the root directory. I had this same issue, hit this page, and none of the answers really helped. So I have 2 files:

~/work/com/domain/pkg/library/A.java

~/work/com/domain/pkg/library/B.java

A.java:

package com.domain.pkg.library;

class A {
}

B.java:

package com.domain.pkg.library;

class B {
  A a = new A();
}

You need to be in ~/work and type:

javac com/domain/pkg/library/B.java

Hope that helps someone.

Upvotes: 4

OscarRyz
OscarRyz

Reputation: 199333

You didn't post your code, so my answer may be wrong, but most likely you're trying to use a constructor in B which uses a string as parameter when there is none defined like that.

Here's my test:

class A {
    B b = new B("b");
}
class B {

}

$javac A.java 

A.java:2: cannot find symbol
symbol  : constructor B(java.lang.String)
location: class B
    B b = new B("b");
          ^
1 error

Looks the same doesn't?

Upvotes: 3

DJ.
DJ.

Reputation: 6784

Likely you have not setup your compilation dependency and classpath properly. If A needs B, you need to compile B first to get B.class and when you compile A, make sure B.class location is in your classpath so the compiler can find it.

Upvotes: 0

Related Questions