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] 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


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:



package com.domain.pkg.library;

class A {

package com.domain.pkg.library;

class B {
  A a = new A();

You need to be in ~/work and type:

javac com/domain/pkg/library/

Hope that helps someone.