Search code examples
eclipse-pluginosgiclassloadereclipse-cdtlinkageerror

LinkageError in eclipse CDT plugin that using OSGI


I'm writing eclipse CDT plugin on windows. I want to write code to an existing file in a project, but I need the code to be formatted automatically. I googled and googled, and found this solution:

CodeFormatter formatter = ToolFactory.createDefaultCodeFormatter(null);
TextEdit formatEdit = formatter.format(CodeFormatter.K_UNKNOWN, source, 0, source.length(), 0, null);
IDocument dc = new Document(source);
formatEdit.copy();
formatEdit.apply(dc);
System.out.println(dc.get());

In order to get this code working(at least partially), I added the following imports:

import org.eclipse.cdt.core.ToolFactory;
import org.eclipse.cdt.core.formatter.CodeFormatter;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.TextEdit;

and the following libraries in the class path and in the plugin.xml runtime definitions:

<classpathentry kind="lib" path="libs/org.eclipse.cdt.core_5.6.0.201402142303.jar"/>
<classpathentry kind="lib" path="libs/org.eclipse.osgi_3.9.1.v20140110-1610.jar"/>
<classpathentry kind="lib" path="libs/org.eclipse.osgi.services_3.3.100.v20130513-1956.jar"/>
<classpathentry kind="lib" path="libs/org.eclipse.osgi.services.source_3.3.100.v20130513-1956.jar"/>
<classpathentry kind="lib" path="libs/org.eclipse.osgi.source_3.9.1.v20140110-1610.jar"/>
<classpathentry kind="lib" path="libs/org.eclipse.osgi.util_3.2.300.v20130513-1956.jar"/>
<classpathentry kind="lib" path="libs/org.eclipse.osgi.util.source_3.2.300.v20130513-1956.jar"/>
<classpathentry kind="lib" path="libs/org.eclipse.text_3.5.300.v20130515-1451.jar"/>

I was sure that with so much dependencies, the code will work - but now i'm debugging, and getting the following exception at runtime:

java.lang.LinkageError: loader constraint violation: loader (instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader) previously initiated loading for a different type with name "org/eclipse/text/edits/TextEdit"

I saw this topic is discussed already, but I myself - didn't find out the solution.

Can anyone help me please?


Solution

  • I found the solution. As @greg-449 answered, I had to add the wanted jar to the Dependencies in the MANIFEST.MF. thanks!!