0

I created a jar file and would like to implement it into my other projects. However, I am getting an error "cannot be resolved to a type." I added the .jar file to the Build Path Libraries within eclipse but the error persists.

EDIT: I tried following the tutorial here: http://www.programcreek.com/2011/07/build-a-java-library-for-yourself/

However, this still does not work.

System.out.println(Simple.add(1,2));

I get the error "Cannot be resolved" on the Simple class name.

EDIT:

package TEST;
public class AccessSimple
{
   public void access()
   {
      System.out.println(Simple.add(1, 2));
   }
}

Here is the class I am using to access my jar file. It is not part of the default package.

cain4355
  • 1,094
  • 1
  • 10
  • 22

2 Answers2

0

Try to do a refresh in the project to see if it works then. Otherwise try clean. Then build and refresh. Finally perform a restart of eclipse

EDIT I read somewhere that if you try Windows–>Preferences–>Java–>Compiler–>Building–>Output folder–>”Rebuild class files modified by others” will work. Give it a try and tell me to search better

Bill Soumakis
  • 11
  • 1
  • 5
  • I did all of those already thinking it may have been a bug in Eclipse. I was even desperate enough to just restart my entire system. – cain4355 Aug 26 '14 at 18:32
0

Check the "Java Complier" version used to build the jar file against the "Java Compiler" version of your project. If the jar you are including was built with a higher version than the compiler version of your project, the compiled class might not be found.

terrywb
  • 3,740
  • 3
  • 25
  • 50
  • They are both using the same version. jdk1.7.0_60 – cain4355 Aug 26 '14 at 18:51
  • Perhaps this will help. Open package explorer in Eclipse. Find your referenced jar. Expand the tree to find the class of interest. Right click on the class you want to use and select "Copy Qualified Name". Paste this class reference to ensure that the reference to the class matches. – terrywb Aug 26 '14 at 19:17
  • Where should I paste the class reference to? – cain4355 Aug 26 '14 at 19:21
  • I assume that the error "cannot be resolved to a type" is a compile error that you are seeing in Eclipse. If that is correct, paste the class name at the point in the code where you are referencing the class. If you are seeing the error at runtime, paste the value in as your class name. java -cp my.jar package.ClassName – terrywb Aug 26 '14 at 19:24
  • This is a compile error within eclipse. Copying the Qualified name only gave me "Simple.class" with the same "cannot be resolved to a type error" – cain4355 Aug 26 '14 at 19:52
  • What package is your Simple class in? Is it in the default package? What class is your test code in? Is it possible that this article describes your situation? http://stackoverflow.com/questions/2193226/how-to-import-a-class-from-default-package – terrywb Aug 26 '14 at 20:08
  • It is in the default package. I followed the tutorial found in the edit of my original post. They had their Simple.java in the default package. While it is possible, I do not want to have to use reflection for this. I need to be able to implement a jar file like any other. – cain4355 Aug 26 '14 at 20:27
  • Could you share your code that references the class Simple? Is it in the default package? – terrywb Aug 26 '14 at 20:38
  • I just made an edit to my original post showing the class I am using to access my jar. It is not part of the default package. – cain4355 Aug 26 '14 at 20:48
  • If you review the discussion in the article I referenced above, you will see that you cannot reference a class in the default package from a class that is in a named package. Consider making a copy of the Simple class and placing it into a named package. – terrywb Aug 26 '14 at 20:51
  • I added the Simple.java to a Test folder. However, I still have the same problem. I tried to view the jar through the Package Explorer again but all I see under Simple.jar is the Test package. No class file. – cain4355 Aug 26 '14 at 21:00
  • Did you add a package delcaration? At the top of Simple.java: package Test; In your referencing class, add import Test.Simple; to the top – terrywb Aug 26 '14 at 21:02
  • I got it. Adding it into a package is what it needed. I just needed to restart eclipse for it to recognize the new jar file for some reason. Thanks! – cain4355 Aug 26 '14 at 21:54