Take the 2-minute tour ×

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


share|improve this question
Could you post your javac command line? –  Andreas_D Nov 25 '09 at 21:15
Could you post the code? There is a change you don't have a constructor that uses a String –  OscarRyz Nov 25 '09 at 21:18
With both classes in the same directory, both not declaring a package (i.e. both are in the default package) add the directory that contains your source files to the javac classpath. –  rsp Nov 25 '09 at 22:27
add comment

3 Answers

up vote 3 down vote accepted

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?

share|improve this answer
add comment

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/B.java

Hope that helps someone.

share|improve this answer
add comment

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.

share|improve this answer
How do you do this? –  Heinrich Schmetterling Nov 25 '09 at 21:34
As far as I know, javac will compile all dependent classes automatically. –  rsp Nov 25 '09 at 22:25
@rsp you are right. It has been a while since I do manual command line compilation, I guess I remembering C compiler behavior :D –  DJ. Nov 25 '09 at 23:19
add comment

Your Answer


