Java “cannot find symbol” – how to import a file in the same directory

java

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:

  • in A, doing "import B;"
  • adding "package myPackage;" to both A and B

Thanks!

Best Solution

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.