Eclipse: Getting error when adding external class / jar files to build path: "XYZ Cannot be resolved to a type"

StackOverflow https://stackoverflow.com/questions/23179675

  •  06-07-2023
  •  | 
  •  

Question

I am working with Java and Eclipse, which are both a bit out of my comfort zone (yup , this is an assignment...).

I have 2 class files which I need to use in my project. So far I have tried:

Attempt #1

I tried adding an external class folder:

  1. In my project folder I have added classes\legacy folder.
  2. In Properties > Java Build Path > Libraries I Clicked on the Add External Class Folder
  3. In the window that opened I selected the classes folder created in step 1.
  4. In my code I have added import legacy.*;

Result:
I can use the classes in the class files but getting the following errors:
(seems to occur when the classes are attempting to use one another)

The project was not built since its build path is incomplete. Cannot find the class file for IshKesher. Fix the build path then try building this project

The type IshKesher cannot be resolved. It is indirectly referenced from required .class files

Note: tried for class folder as well - same results.

enter image description here

Attempt #2

Someone suggested that using jar files should be easier, so I gave it a try:

  1. Created a jar file containing the class files: jar cvf classes.jar ContactsList.class IshKesher.class
  2. Added the jar file to the build path using this tutorial.

Result:
Getting an error for each usage of classes in the jar file:

XYZ Cannot be resolved to a type

enter image description here

Any Ideas how make the class files available in my code?


EDIT:
It turned out that Attempt #2 worked once using my source files in the default package (cannot figure out why though...).

Was it helpful?

Solution

Try my instructions.

If you don't want to include copy of Borat.jar, but only want to reference it from an external folder, then just do this one step -

No copy step -

Your java project > Right click > Build path > Configure 
build path > add EXTERNAL jars button > select your jar 
> ok. 

If you want that jar to become a part of your java app, ie create a copy of the jar in your java app, then use the steps below.

Step 1- Create a lib folder in your app to store all jars

Your java project > Right click > New Folder > Folder Name = lib

Step 2 - Import all copies of the necessary jars into lib folder

Your java project > lib folder > Right click > import > General --> 
File System > From Directory = The directory where you stored your jar files.
> ...You should now see the jars in that folder. Lets say we want to import 
Borat.jar > Finish.

Step 3 - To check if step 2 is done correctly

Your java project > lib folder > Expand the lib folder to see its 
contents. Borat.jar should be listed there. Nice ! 

Step 4 - Finally, to add the jars to the project

Your java project > lib folder > Right click > Build path > Configure 
build path > add jars button > expand your project > expand lib folder
> Select Borat.jar > ok. 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top