Adding a library to .classpath file in Eclipse

We are using Buildship’s plugin in Eclipse. We are using a protobuf compiler as part of the build that generates .class files from .proto files. The build works fine from the command line (it adds the directory containing the generated class files to the classpath) and everything builds and executes.

When doing a Gradle/Refresh Gradle Project in Eclipse, it regenerates the .classpath file without including that directory as a lib.

I have seen countless posts describing how to add a library in the build.gradle file but no matter what I try it doesn’t work. It is simple to add a ‘container’ classpath entry, but seems to be very difficult to add a ‘lib’ classpath entry. It baffles me.

Can someone please describe the steps that I need to take to add this to the .classpath file?

<classpathentry kind="lib" path="/conductor-grpc/build/classes/java/main"/>

I am using Eclipse Neon 4.6.2 with Buildship plugin 3.1.0.

Isn’t it good enough to declare the lib entry in the Gradle classpath container? There are a few examples of how to do it here.

Not sure how to declare the lib entry in the gradle classpath container - how to do that?. Using the link you referenced, I tried the below. I could see, using println statements, that the library was added to entries but there was no affect on the .classpath file (the ‘lib’ entry did not go in there even though it was in the ‘entries’ object).

import org.gradle.plugins.ide.eclipse.model.Library
apply plugin: ‘eclipse’

eclipse {
classpath {
file {
whenMerged {
entries += new Library(fileReference(file(’/conductor- grpc/build/classes/java/main’)))
}
}
}
}

In Buildship, the whenMerged block does not modify the .classpath file but the content of the Gradle classpath container (i.e. the content of the Project and External Dependencies classpath entry, represented by <classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>).

Thanks, at least that explains why it did not alter the .classpath file. But I also do not see that folder in the Project and External Dependencies. But in any case, what I really want to do is to add a ‘lib’ entry in the .classpath file. It is so simple to add a ‘con’ entry but difficult if not impossible to add a ‘lib’ entry. Is it possible? If not, how do I have Eclipse resolve the compile errors I get because the .class files are generated from the .proto files but I can’t seem to add the folder containing the .class files to the classpath of projects that need them.

I don’t have a recommendation on how to modify the .classpath file. But adding a library entry to the Gradle classpath container should have the same effect. I managed to load a class from an external folder using a snippet similar to yours:

import org.gradle.plugins.ide.eclipse.model.Library
eclipse {
    classpath {
        file {
            whenMerged {
                 entries += new Library(fileReference(file('check/classes/main')))
            }
        }
    }
}

Not sure what I am doing differently bcoz it doesn’t work for me. Are you using the Gradle/Refresh Gradle Project to cause the whenMerged to be called? Can you actually see the check/classes/main folder in the Project and External Dependencies in the Eclipse UI?

Yes, it works for me. I’ve copied a simple class from another project to the check/classes/main folder to verify the behavior.

Interesting. I am using Eclipse Neon for Windows. I wonder if the Mac version of Eclipse works for this functionality but the Windows version does not…

Thanks for spending time on this. I appreciate it.

@rickfish1 / @donat I hope you guys don’t mind me being a Butt-inski.

But what y’all are discussing is a very useful thing to be able to do. So I wanted to see if I could do it.

Luckily, I was able to get it working on Windows with Eclipse 2019.12…

You might need to explicitly run the :eclipse task to get it to take, rickfish1. Or you might not. That’s what I did, though, just for good measure.

Many Thanks, donat, for showing us how it’s done :+1:

1 Like

Thanks guys. The problem was simply that I was trying to reference the classes folder with the project name. In other words, I was using '/conductor- grpc/build/classes/java/main’. Instead I should have used 'build/classes/java/main’. Once I made the change it worked.

Your examples pointed me to the problem. Thanks so much for your help.

Anybody can help me telling me how to add an linkedresource to my .classpath under Eclipse 2019-06?.
I have followed all instructions to create linked resources and it is properly generated to my .project but no way to get it added to my .classpath: I have refreshed gradle project, built using eclipseClasspath and eclipse tasks and no result.