tags:

views:

229

answers:

2

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!

A: 

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.

DJ
How do you do this?
Heinrich Schmetterling
As far as I know, javac will compile all dependent classes automatically.
rsp
@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
+3  A: 

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?

OscarRyz