Your_Eye
Your_Eye

Reputation: 31

Eclipse Plugin - adding a jar used by eclipse itself

I'm trying to write an Eclipse-Plugin using Eclipse. The plugin uses org.eclipse.jdt.core. At some point in my code, I'm getting an object of CompilcationUnit from the menus-action-handlers, and I want to find the java-file's path that was clicked on using my plugin.

At first, on the line getting an object of type "CompilcationUnit", I got a NoClassDefFoundError (I referenced the org.eclipse.jdt.core.jar that was in my eclipse/plugins folder). This thread solved that issue: Eclipse plugin: java.lang.NoClassDefFoundError

Similar solution found it: Java eclipse : Importing jar file in eclipse plugin project

Now, I have a different error. I'm trying to verify the element is of type CompilationUnit and then use it. I wrote the following code:

System.out.println(element.getClass());
System.out.println(org.eclipse.jdt.internal.core.CompilationUnit.class);

System.out.println(element instanceof org.eclipse.jdt.internal.core.CompilationUnit);
    
System.out.println(element.getClass().getPackage());
System.out.println(org.eclipse.jdt.internal.core.CompilationUnit.class.getPackage());

System.out.println(element.getClass().getProtectionDomain().getCodeSource().getLocation());
System.out.println(org.eclipse.jdt.internal.core.CompilationUnit.class.getProtectionDomain().getCodeSource().getLocation());

org.eclipse.jdt.internal.core.CompilationUnit cu = (org.eclipse.jdt.internal.core.CompilationUnit)element;

The output is this:

class org.eclipse.jdt.internal.core.CompilationUnit

class org.eclipse.jdt.internal.core.CompilationUnit

false

package org.eclipse.jdt.internal.core

package org.eclipse.jdt.internal.core

file:/C:/Program Files/Eclipse/plugins/org.eclipse.jdt.core_3.11.1.v20150902-1521.jar

file:/D:/Svn/OrderingTR/Source/ClassMethodsRearranger/external/org.eclipse.jdt.core_3.11.1.v20150902-1521.jar

And then I get an exception:

org.eclipse.e4.core.di.InjectionException: java.lang.ClassCastException: org.eclipse.jdt.internal.core.CompilationUnit cannot be cast to org.eclipse.jdt.internal.core.CompilationUnit

I do not understand this. It seems the element is of the same type that my code is referencing. Only my code references the type in ile:/D:/Svn/OrderingTR/Source/ClassMethodsRearranger/external/org.eclipse.jdt.core_3.11.1.v20150902-1521.jar

and the element is of the same type of the same jar, only located in file:/C:/Program Files/Eclipse/plugins/org.eclipse.jdt.core_3.11.1.v20150902-1521.jar.

Now, I need to reference the file in D:/Svn/... because that is the project's jar, and it is needed for eclipse plugins (otherwise I get the NoClassDefFoundError solved earlier). But I am unable to use the element I get from the menu-actions-handlers (I believe because it originated in a different jar file). I need to use that element. What can I do? Has someone encountered this kind of problem? I searched and searched and found nothing on the subject... It seems it is specific to eclipse plugins and using the jdt.core.

Thank you very much, I really appreciate the long read.

Upvotes: 0

Views: 121

Answers (1)

Your_Eye
Your_Eye

Reputation: 31

I eventually solved it by adding the reference in the MANIFEST.MF. At the dependencies, I added org.eclipse.jdt.core in the Required Plug-ins.

Upvotes: 1

Related Questions