1

I have already linked the necessary jar file to the project that I'm building right now. But when I tried to call methods supported by that .jar library, Eclipse told me that "this method can't be resolved". Now what should I do?

Is there any syntax, such as "import xxx.jar;" that I have to add to the source to make it work?

Again, sorry I am sort of new to java. I could easily do that on linux with a tag but can't see how to accomplish the same thing with eclipse. I know it is confusing without pictures but I'm not allowed to post any since I just registered.

lwang456
  • 55
  • 2
  • 3
  • 5
  • You have to set your classpath to contain the jar that contains the class that contains the method. You have to make sure you're spelling the method correctly, and passing in the correct types of arguments. Can you provide an [SSCCE](http://sscce.org) that generates the error? – Kevin Workman Jan 09 '14 at 03:18
  • 2
    you need to add this jar into your build path, then use `import` to use API in this jar. in java, it is not confusing to load a jar into your application – Rugal Jan 09 '14 at 03:19
  • @Rugal the error is a "method cannot be resolved" error, which suggests his problem is with either spelling the method wrong or passing in the wrong arguments. If it were a classpath or import problem, he'd be getting a different error. – Kevin Workman Jan 09 '14 at 03:22
  • no. `method cannot be resolved` just say compiler can not find relevant source or compiled `.class` or `jar`. – Rugal Jan 09 '14 at 03:25
  • is there any error warning on `import` line ? – Rugal Jan 09 '14 at 03:25
  • @RugalI think my problem should then be "what is the syntax for an appropriate import statement to use API in that jar?" – lwang456 Jan 09 '14 at 03:33

1 Answers1

2

Method cannot be resolved simply means Eclipse cannot find the method definition. You have probably not loaded the jar file correctly in eclipse. To do so, add the Jar file as a reference library in your Eclipse project. Also, in your source code, import the necessary class from the jar file using import [classname].

You can actually open the jar file in eclipse and look inside it to find the correct class you need. This is really helpful to get the syntax write. An import may look like this import com.myjar.className To add a jar file look here

enter image description here

Community
  • 1
  • 1
Programmer
  • 6,565
  • 25
  • 78
  • 125
  • Thanks for answering. I'm still confused about the import statement. I have successfully added the jar file as a reference library in my project, but the actual location of the jar file is on my desktop. Therefore are you suggesting me to write the import statement as "import Desktop/xxx.jar/xxx.class"? – lwang456 Jan 09 '14 at 03:57
  • No. Try clicking on the jar file in eclipse. You should be able to see the classes within it. You have to import the classes. – Programmer Jan 09 '14 at 03:59
  • reference to the desktop is not required – Programmer Jan 09 '14 at 03:59
  • Well I guess I explained it wrong. I was asking what should be the correct syntax for importing. Say I want to import a.class that is in my jar file, should the syntax be "import a" or anything else? – lwang456 Jan 09 '14 at 04:01
  • Check out my image. org.apache.log4j is a package within the jar file log4j128.jar . That org.apache.log4j has classes inside it. To import a class do `import org.apache.log4j.className` – Programmer Jan 09 '14 at 05:36