0

I'm working on updating some Java code for my job, using Eclipse. In this project, there is a JAR file, stored in a 'resources' folder within the project folder, being imported by the main class. This JAR file contains a class we need to use, within another class. So, the import statement looks like:

import pkg.Class.InnerClass;

I can't actually put the names online, for confidentiality. This InnerClass has a method, which we'll call setState(boolean b). In our main class, we need to use this method... But when I type it out, a red line appears under the invocation, with the error message,

"The method setState(boolean) is undefined for the type Class.InnerClass."

This is clearly false! Almost all of the other methods in InnerClass work just fine, and it only seems to be an error with this specific class. I've checked and double-checked the build path configuration, and there isn't anything abnormal about it. I cannot change the JAR file, but I can see the contents using a decompiler plugin. Does anyone know something I don't, or has someone run into this problem before and been able to work around it somehow?

Edit: Here's a sketch of what the Class.java file looks like:

package pkg;

import ...;

public class Class {

    public class InnerClass {

        public void setState( boolean paramBoolean ) {

            ...

        }

        public void setOtherState( boolean paramBoolean ) {

            ...

        }

    }

}
p.koch
  • 183
  • 1
  • 9
  • 5
    Is setState(boolean) private? – Thomas Jun 14 '12 at 14:32
  • Since we can't see your build path configuration, that's also a suspect. – Dave Newton Jun 14 '12 at 14:34
  • Could you tell us how are you calling `setState` method on `Class.InnerClass` ? – RP- Jun 14 '12 at 14:35
  • 1
    1. setState(boolean) is public, and other public methods work fine. 2. It is my assumption that the buildpath isn't the problem, because other methods in the InnerClass work properly. 3. Sure: InnerClass obj = new InnerClass(); obj.setState(true); – p.koch Jun 14 '12 at 14:36
  • Then please show both the InnerClass declaration and how it is used (both when it works and doesn't) – adarshr Jun 14 '12 at 14:37
  • An example of when it does work looks the exact same, but with a different method. Literally five lines later, I have obj.setOtherState(false); and it works. – p.koch Jun 14 '12 at 14:39
  • does context assist suggest the method when typing obj.ctrl+space? – guido Jun 14 '12 at 14:42
  • Perhaps because the 6th line is outside the scope of InnerClass's visibility. If you have a method-local inner class, for example. – adarshr Jun 14 '12 at 14:42
  • Context assist doesn't suggest it, no. It suggests the other methods... But not the one I need. – p.koch Jun 14 '12 at 14:48
  • 3
    Click on the "Link with Editor" icon button in the package editor. Then select the InnerClass import in the Java editor containing your source code, right-click and choose "Open Declaration". Check in the package explorer that the class file it has selected is in the jar you expect. You might have two jars in your build path that both contain the same class, but in a different version. – JB Nizet Jun 14 '12 at 15:02
  • Aaaah, thank you, JB! Deep, deep down in one of my other imported JAR files was an old version of the pkg package, before it had the setState method. So now, how do I tell it to link to the new one instead of the old one? – p.koch Jun 14 '12 at 15:13
  • Just kidding, it turns out the old JAR was completely unnecessary... I removed it from the buildpath and the entire project became free of errors. Thank you so much! – p.koch Jun 14 '12 at 15:20

1 Answers1

0

Here's a test to determine whether the problem is really to do with Eclipse, or some issue with JAR file.

  • Exit Eclipse and start up your favorite text editor.
  • Write a small test class that uses the "missing" method in the inner class.
  • Compile it from the command line using the javac command, with the JAR on your compilation classpath.
  • Then copy it into your Eclipse workspace and try to compile it there.

This should give you some clues as to where the problem lies.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Oddly, I've never gotten the javac command to work independently on this computer, so I've never tried it. But that's an issue for another day. Hahaha. – p.koch Jun 14 '12 at 15:21
  • You need to have C:\Program Files\Java\jdkx.x.x\bin; in the PATH environment variable for this to work. – JustBeingHelpful Jan 03 '15 at 07:25