0

I'm trying to write a simple ant build to compile a project. The project is in eclipse and there it compiles successfully (with the eclipse-compiler). But with ant (using javac) it appears an error and i don't know how to resolve it.

Structure of the used jar:

  • com
    • xxx
      • a <= package
        • b
      • a.class

Codeblock of my class:

Object o = com.xxx.a.b.method();
                    ^

The exception of ant is:

error: cannot find symbol

symbol: variable b

location: class a

I think eclipse uses the package first to try to compile the code. javac seems to think that a is the class.

Is there a way to resolve the problem without changing the jar?

Community
  • 1
  • 1
Benjamin Schüller
  • 2,104
  • 1
  • 17
  • 29
  • maybe you should abide to the Java convention: class names start with uppercase, packages all lowercase...I just found: https://stackoverflow.com/q/9584086/85421 – user85421 Sep 05 '18 at 12:04
  • This sounds like `javac` vs. Eclipse compiler and has nothing to do with Ant. Is it possible to reproduce this issue with `javac` on the command line (and if so, with which JDK version)? – howlger Sep 05 '18 at 12:17
  • I tried it with pure javac. Same error. So ant has nothing to do with it. I'm using Java 8. – Benjamin Schüller Sep 05 '18 at 12:33
  • Update the title of this question, and remove such ambiguities from your sources. – nitind Sep 05 '18 at 17:04

2 Answers2

0

It looks like either package name is different or you have multiple class files of the same name. I would suggest checking the import statements and adding the specific jar file to classpath while compiling using javac or ant command.

To find the exact jar file, use ctrl+T then paste your class name in the box and it will tell you the jar file. Add that jar file to your ant classpath and build.

Atul
  • 1,536
  • 3
  • 21
  • 37
0

I didn't find anything in the Java Language Specification that this is an error, so it might be a javac bug.

Since it is a javac vs. Eclipse compiler thing, try one of the following:

  • Use the Eclipse compiler in the Ant script
  • If it is a javac bug, the bug may be fixed in a newer (update) JDK version
  • If your code does not directly reference class com.xxx.a, compile the code with the JAR in which the class com.xxx.a was removed
howlger
  • 31,050
  • 11
  • 59
  • 99
  • Positively speaking, JLS sections §6.5.1 and §6.5.5.2 should allow correctly resolving a known-to-be-typename to the desired type. I tried a few examples where javac and ecj seem to agree on ignoring these rules. In cases like this JLS seems to be overruled by reality. If OTOH a difference between compilers is detected, a fix towards the specified behavior in one of the compilers should be more likely. Hence: is there a case where compilers _disagree_? – Stephan Herrmann Sep 08 '18 at 12:27