-1

my question is almost the same as this one : java packages: cannot find symbol

Suppose you have two classes A and B, declared respectively in the files A.java and B.java, with B used in the class A. Then the command line "javac A.java" will work perfectly. But somehow if you add both A and B in the same package (adding the line : "package toto;" at the begining of each file), then "javac A.java" will give the error that the symbol "B" cannnot be found.

The solution given above (java packages: cannot find symbol) was to call the compilation line : "javac A.java B.java" and this works. I would like to know if there is a solution to make it work, but compiling the java files one at a time. (I use java 7).

Thanks in advance

Community
  • 1
  • 1
Archimondain
  • 374
  • 2
  • 17

2 Answers2

0

A uses B and B does not use A, right? This is what I guess from your question. Based on this you should first compile B and then A.

javac B.java //generates B.class
javac -cp . A.java 
//It is important to add current-folder (location of B.class) in classpath

Above doens't uses package declaration, for working with package, I think the solution posted in above link should work fine:

Suppose your dir structure is :

main -|
     toto -|
           A.java, B.java

So inside main directory:

$main: javac -sourcepath ./toto -d ./toto B.java
$main: javac cp ./toto -sourcepath ./toto -d ./toto A.java

Above uses toto for both source and bin folder.

harsh
  • 7,502
  • 3
  • 31
  • 32
0

You have to move A.java and B.java into toto folder. See: Managing Source and Class Files

scb
  • 82
  • 1
  • 2