Java: Cannot find symbol error

Started by
3 comments, last by Belgium 11 years, 3 months ago

This has been driving me nuts for awhile. I have an overloaded method which is located in a jar file. This jar file has a number of common classes with are used by many other jar files. The common jar file compiles without error. I'll be calling it common.jar.

Whenever I try to use that method from detroit.jar I receive a "cannot find symbol" error. Other usages of the method compile fine, just one specific version of the method gets the error message.

For example;
1. Foo.print(2, 3);
2. Foo.print(new File("x.txt"), 42);
3. Foo.print(false);
4. Foo.print("hi!");

Methods 1, 2 & 3 compile and work. So my imports are there. However, method 4 causes detroit.jar to fail compiling with the "cannot find symbol" error. I know the method for #4 is there. When I have Netbeans "goto source" it correctly finds the source method for #4. When I "find usages" for #4 it correctly identifies the non-compiling usage of it so Netbeans knows it exists too. The only difference between the first three working methods and the last method is the last method is new which seems to be key.

When I try to duplicate this problem from within a different jar, say chicago.jar, it compiles just fine. So, there seems to be something specific between common.jar and detroit.jar which is conspiring to tick me off. I've tried clean rebuild for both, no avail. Renaming the #4 method gets the same error. No changes I make in common.jar are being seen by detroit.jar. However, those changes ARE seen by chicago.jar. There are other jar files which detroit.jar imports and changes to those jar files compile/work just fine. wtf.

Ideas?

Advertisement

Have you included your .jar in the project settings?

See my game dev blog: http://gamedev4hobby.blogspot.fi/

.

Instead of trusting your IDE's idea of "view source" and "find usages", which doesn't necessarily match runtime classpaths, you should look at actual class loading by running your tests with breakpoints in strategic places like java.lang.Class constructors. Where are you loading classes from? Did you put the "right" version of the problem class there?<br /><br />Another possible approach: run tests from a command prompt, with loose .class files instead of jars; they should work, and then you can reintroduce jar archives into your classpath one at a time (after rebuilding and checking their content properly) to see when the problem reappears.

Omae Wa Mou Shindeiru

Thanks, LorenzoGatti. I didn't quite understand what you were referring to and am still too ignorant to know if you were telling me the answer but you did send me down a path of investigation which resulted in a fix.

Anyways, I have made the problem go away. I believe the problem is related to the build order of the jar files. When I put "common.jar" at the top of the compile list the problem goes away.

My ignorant theory:

common.jar is used in many other jar files. Many of those other jar files are also included in the problem file, "detroit.jar". When I make a change to common.jar it isn't being seen by detroit.jar because an earlier .jar file has already been compiled which includes the non-editted version of the classes in common.jar. It's this old version of the classes in common.jar that are being seen by detroit.jar. The class is there but the new method is not. New classes and methods are seen but new methods of previously existing classes are not.

Odd thing is, after moving common.jar to the top of the compile list, compiling successfully, then moving common.jar to the bottom of the compile list, it still compiles fine so there's some understanding I still don't have here. I can duplicate and resolve the problem at will at this point by adding new methods.

So, other than ensuring common.jar is always at the top of the compile list, is there a better way of avoiding this problem?

This topic is closed to new replies.

Advertisement