Add .class file in Eclipse

Adrian Rodriguez :

I know this is a noob question and maybe you can think that is a duplicate question, but none of the solutions it worked for me. Besides I have this problem since 2 days. The thing is, I am trying to add a .class file in java in Eclipse. This is the error:

enter image description here


This is the project structure
enter image description here

I added the file from a class folder and it didn't work.

enter image description here


I also tried to import the .class file in a zip. It didn't work also. The curious thing is that, when I autocomplete the text of the import, Eclipse recognizes it. The file should be added in librerias.estructurasDeDatos.deDispersion . I am using Eclipse 2018-12 (4.10.0). And also, I saw a lot of solutions using Maven, but I am not using it. Also, I would like to know, why in the Package Explorer view I can see .java files and not .class files, because my TablaHash.class it is added in the project. Any help would be good. I am sure it could be a silly solution, but I can't see what I am doing wrong.

enter image description here


EDIT: I have changed lib folder to this and it doesn`t work.

enter image description here

user85421 :

Let's suppose I have a project so.lib where I have the Source for TablaHash.java:

package librerias.estructurasDeDatos.deDispersion;

public class TablaHash {

    public String test() {
        return "OK, I'm here";
    }
}

in another project so.test I have a Main class that uses previous class:

package so.test;

import librerias.estructurasDeDatos.deDispersion.TablaHash;


public class Main {

    public static void main(String[] args) {
        var tabla = new TablaHash();
        System.out.println(tabla.test());
    }
}

Option 1 - Use first project in second project

Add the first (library) project to the classpath of the Projects tab in the second porject settings.

Project structures and settings of so.test:

Project Structure Project Settings


Option 2 - Copy class file

Copy the class file(s) and directories in a lib folder of second project. Add the lib folder to the classpath in the Libraries tab. (I used lib, but the folder can be any (valid) name)

Project structures and settings of so.test:

Project Structure Project Settings

Note: the folder structure/hierarchie inside lib is the same as from the project where the class file was located (same as the package hierarchie):

Folder Structure

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25345&siteId=1