0

I have an A.java class which uses B.java class as an object.

When I compile A.java class, it throws a compile error message, since the Java compiler can not reference the B.java object at all. So, here is my question:

How do I compile A.java class if it includes another B.java class?

Eclipse is a great tool, but this tool is not useful when I need to compile a Java file for Java beans.

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
Seho Lee
  • 4,048
  • 9
  • 32
  • 42
  • So are you saying you're having this error when compiling with eclipse, with the command line, or both? There seems to be some confusion amongst us on the scope of your question. – jamesmortensen Apr 12 '12 at 06:46

4 Answers4

6

You haven't explained how you're trying to compile A.java, or whether you've already compiled B.java. If you haven't compiled either of them yet, just compile them both together, e.g.

javac -d bin path/to/A.java path/to/B.java

If you've already compiled B, you need to make sure you've got the classpath right, e.g.

javac -d bin -cp path/to/Broot path/to/A.java

Note that the classpath value shouldn't be the B.class file itself, nor even the directory containing B.class - but the root of the output hierarchy. So if B is in package foo.bar, and B.class is in directory /x/y/z/foo/bar you'd write:

javac -d bin -cp /x/y/z path/to/A.java
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

The error seems to be that the class B is not yet compiled. Could you check class B for errors, compile B, and then go for A?

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
Akash Yadav
  • 2,411
  • 20
  • 32
0

make sure that the dependency(jar file) and the current directory is in the classpath when you compile a class.

Jasonw
  • 5,054
  • 7
  • 43
  • 48
0

This is a duplicate of the question: Can I use JAVAC to compile a project with multiple files and directories?

Depending on the type of object B you must add it to the classpath. If it's in a jar file (library) you should the -c option on command line. For adding other source files use the -s option.

Once you start bigger projects it will be hard to keep doing this manually on the command line. Look at a proper IDE like eclipse. It does all that for you. You only have to set the classpath in the properties once and then you can just compile with the click of some menu options (or set automatic building or use shortcut keys).

Community
  • 1
  • 1
hcpl
  • 17,382
  • 7
  • 72
  • 73
  • Actually, it's not a duplicate, unless that question included eclipse. The OP specifically mentioned that "Eclipse is a great tool, but this tool is not useful when I need to compile a Java file for Java beans." This implies the OP is experiencing this problem in eclipse. – jamesmortensen Apr 12 '12 at 06:44