How to call a plugin method from another plugin in Eclipse? - java

I'm having the following situation:
I want to extend the functionality of a given plugin A (I have it's source code and start it by running the project as an Eclipse Application which opens a new Eclipse IDE which provides the plugins functionality) with an plugin B I am writing.
My plugin does run when I run it as a Java Application. Let's assume it just prints Hello World in the console. What I want is that I can call the function which does that from plugin A.
What I did:
I added my plugin B to plugin As Required Plugin-Ins.
I create an instance of the class which implements the Hello World-print and call the function inside a method of plugin A (I also tried to make the method static and call it without creating an instance which resulted in exactly the same errors).
I created an Extension Point in plugin B and added it as an Extension in plugin A. I just set the ID and name in the Extension Point.
What happens:
When the instance of the class in plugin B should be created, the program crashes with this error:
java.lang.NoClassDefFoundError: de/name_of/plugin_b/package/ClassName
[...]
Caused by: java.lang.ClassNotFoundException: de.name_of.plugin_b.package.ClassName cannot be found by de.name_of.plugin_a.package_1.0.0.qualifier
I guess I'm missing something imporant - can someone help me out on what it is?
Edit 2:
I've just read that I have to add "." to the classpath. Seems like this solved the issue! Thanks for making me dig deeper into the manifest, greg!
I do get a different error now tho, which also seems to be connected to me making mistakes when creating the plugin as I do not get this error when I run plugin B as a Java Application.
java.lang.NoSuchMethodError: org.apache.lucene.store.FSDirectory.open(Ljava/nio/file/Path;)Lorg/apache/lucene/store/FSDirectory;
The problem is, tho, that this method does exists (see lucene API here).
As seen in the manifest, I added the lucene-jars to the dependencies of plugin B.

You need to include every package that other plugins use in the Export-Package section of your plugin. In the MANIFEST.MF editor this is on the 'Runtime' tab in the 'Exported Packages' section.
You don't need an extension point to make this work.

Related

NoSuchMethodError after cleaning the project

I'm currently getting this error:
java.lang.NoSuchMethodError: org.json.JSONObject.keySet()Ljava/util/Set;
at ee.ut.cs.Parser.accessLint(Parser.java:39)
I have tried cleaning the project to no awail.
I suspect I have an error in the src/plugin/parse-htmlraw/build.xml while creating the jar file but I'm not certain. I understand that this error is because the function does not exist at runtime, but the object is created which means that the class is there, just not that function. I decompiled the .class file in created jar and it has the necessary functions.
Code is available at https://github.com/jaansusi/WCAGgrader
Q: What is wrong with the build that produces this error?
The problem is that even if I put the necessary class files in the jar I create, they are not linked correctly and the class that's called in the jar can't locate functions inside the other classes. The class object JSONObject is created but the functions inside the JSONObject class can't be found.
If you do not find the problematic version, there is a possibility you get it (especially if you are using Spring) from the following dependency -
<artifactId>android-json</artifactId>
<groupId>com.vaadin.external.google</groupId>
excluding it worked for me,
An easy way of analyzing dependencies is the maven-helper plugin in Intellij, see here
Check for the version you have used.
There might be a case where 2 different versions are being used which in turn causes this error.
To their own maven local repository com\Google\code\gson\gson, see if there are two or more version about json, will have to do is to delete the old, and remember to look at any other place in the project is introduced into the old version of the dependence, if any, change the old version of the dependence to the new version is perfectly solved this problem

NoSuchMethod Error in Java-Solved: Multiple projects added in POM.xml as dependencies had same class name and same method. Renamed them

I have a listener program that triggers a main program based on the below conditions
If the database has new records
If the time from last run is 10 minutes.
This is done using ScheduledExecutorService of Java (https://stackoverflow.com/a/26043250/1930402 helped out).
So, I am calling a perfectly working program from this Listener, located in another Project and another Folder. That is, I am calling project B from project A. To enable this, I added a dependency of Project B in Project A's POM.xml.
There is an error in one of the classes of Project B, and it is NoSuchMethodError. I can see the method right there. From the answers of this post, https://stackoverflow.com/a/24902026/1930402, I understand that this particular class might not be there in my build path. I am not sure of this.
Can anyone advise?
Note: Project B runs without any issues when it is run independently.
Java Doc tells:
"Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed."
How can the definition of my class change while running using a static instance?
This is my stack:
java.lang.NoSuchMethodError:a.b.c.StringUtils.formatDateToString(Ljava/lang/Object;)Ljava/lang/String;
So, these are the key points: Maven Dependency, ScheduledExecutorService, Static Class.
The reason for the error was that same class names and same package names were present in Project A and Project B and a set of other projects which were being used. Once I made each of them unique, things started working fine.

IntelliJ won't accept FileTypeFactory

I am attempting to follow the Language and File Type tutorial for JetBrains IntelliJ
It worked, once. Now (whatever I do) I receive an assertion failure stating that it was expecting an instance of FileTypeFactory and got my SimpleFileTypeFactory
My public class SimpleFileTypeFactory extends com.intellij.openapi.fileTypes.FileTypeFactory so I'm not sure how to react to this ...
Caused by: java.lang.AssertionError: Expected: class com.intellij.openapi.fileTypes.FileTypeFactory; Actual: class bengie.idea.SimpleFileTypeFactory
at com.intellij.openapi.extensions.impl.ExtensionPointImpl.assertClass(ExtensionPointImpl.java:408)
at com.intellij.openapi.extensions.impl.ExtensionPointImpl.processAdapters(ExtensionPointImpl.java:242)
at com.intellij.openapi.extensions.impl.ExtensionPointImpl.getExtensions(ExtensionPointImpl.java:185)
... especially when Google has no matches for "Expected: class com.intellij.openapi.fileTypes.FileTypeFactory; Actual"
Has anyone resolved this sort of thing?
I was depending on a Maven module (actually, two of them) The rest of my project is built with Maven so it seemed logical and looked like it would work. I didn't remember the build seizing up when I added them, so I had forgotten about it. Removing the dependency, starting a debug session, uninstalling the plugin, rinse, repeat and I eventually stopped seeing error messages.
I've since stuffed the maven modules into a jar-with-dependencies and told the IntelliJ plugin module to load the resulting .jar No problems so far - Yay!

Grails 2.1.1 - How to develop a plugin with an AstTransformer?

I want to replace the auto injected log object, which is of type org.apache.commons.logging.Log with an object of type org.slf4j.Logger, so that I can use it properly with Logback.
Therefore I need to create a ...Transformer class (written in Java) - that's what I got from Graeme Rocher over at the "grails-user" mailing list. I'm also aware that I have to pack this ...Transformer class within a plugin and make it a *.jar archive which I can load within the lib/ folder of the plugin. But I guess I'm doing something wrong here as I have the class, along with a META-INF folder which contains the MANIFEST.MF file as well as another folder services which holds the following file org.codehaus.groovy.transform.ASTTransformation which holds just one String: the canonical name of the ...Transformer class.
Now, if I try to do a grails clean everything is fine, BUT if I try to run grails package-plugin the console comes up with a java.lang.ClassNotFoundException.
Clipping from Stacktrace:
| Packaging Grails application...
| Error Fatal error during compilation org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Could not instantiate global transform class my.package.ast.LoggingTransformation specified at jar:file:/C:/Source/MyGrailsAST/lib/replace-logging-logback-ast.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation because of exception java.lang.ClassNotFoundException: my.package.ast.LoggingTransformation
1 error
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Could not instantiate global transform class my.package.ast.LoggingTransformation specified at jar:file:/C:/Source/MyGrailsAST/lib/replace-logging-logback-ast.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation because of exception java.lang.ClassNotFoundException: my.package.ast.LoggingTransformation
Does anybody have some experience with Grails plugins which handle with AstTransformer and could give me some advice on this? Is there a good tutorial out there which I haven't seen so far?
Please let me know ;)
so, after some research, browsing and finally asking at the Grails Mailing List (see the mailing list archives at: http://grails.1312388.n4.nabble.com/Grails-user-f1312389.html I found a solution.
my goal was to create a Globals ASTTransformation, to inject a org.slf4j.Logger object instead of the usual org.apache.commons.logging.Log object into every Artefact class without annotation.
so, here are the steps:
I created Java class similar to https://github.com/grails/grails-core/blob/master/grails-logging/src/main/groovy/org/codehaus/groovy/grails/compiler/logging/LoggingTransformer.java but with my own implementation of the org.slf4j.Logger object. It is crucial that you place the Java.class under the following package: org.codehaus.groovy.grails.compiler as
Grails scans for classes that are annotated with #AstTransformer in this package. (Graeme Rocher)
and pack it into a JAR along with its MANIFEST.MF file within the META-INF/ folder. A META-INF/services directory with all its stuff is not needed as Graeme Rocher stated:
You do not need the META-INF/services stuff and I would remove it as it is probably complicating matters.
So, I guess this statement was more related to my specific problem as I only have one #AstTransformer class within my plugin, but that's just a guess. And I haven't searched for further informations on this topic. Maybe some other developer here who needs this could do some research and share his solution within this thread...
The JAR should be imported to the plugin and placed under the lib/ directory. After this you should be able to do grails clean, grails compile and grails package-plugin.
If you want to replace the log implementation, as I did, you should exclude the grails-logging and grails-plugin-log4j JARs from your designated project's classpath. This is done in the BuildConfig.groovy file:
inherits("global") {
excludes "grails-plugin-log4j", "grails-logging"
}
Now install your plugin grails install-plugin \path\to\plugin.zip and everthing should work as expected.
Hope this helps...

The activator for bundle is invalid

I'm trying to create a simple plugin in eclipse. When I run the application, I see this error in log file:
org.osgi.framework.BundleException : The activator for bundle
org.x.y.Activator for bundle org.x.y is invalid.
Do you have any idea about this error?
Check your build.properties section
If it doesn't properly specify what's supposed to be in the final binary result, it will not work. Check the .class files are where the MANIFEST.MF says they will be.
from EclipseZone, another reason for this error message:
If you see a message in the log like
The activator org.example.FooActivator for bundle org.example.foo is invalid
, then this usually means there has been a ClassNotFoundException trying to load the class in the first place, before it's even got to the start() method.
penguru adds:
The error occurs when I try create a new object from any other class in the constructor of activator class. Isn't it legal to create an object in activator plugin ?
If that class if from another plugin which has not yet been "activated", that could be your problem.
If that class is not found, that would also invalidate your plugin activator.
Basic advice: you may be better off with your initializations done in the start() method of Activator rather than its constructor.
I also faced same issue while importing plugins from different workspace. Basically, it is the bundle classpath where the framework looks for while loading the classes. When you import to a different workspace, make sure you change the class path to point to appropriate location i.e. where the class file are present.
After modifying the classpath try to clean and re-build and re-run. It should work..hopefully..
OK, I hate to be captain obvious here, but I've made this mistake before. This can also happen when you forget to extend BundleActivator.
If you have move the eclipse workspace to a new path, then you should use the project->clean before your plugin build, Or you would meet this problem.
I spent some time with this problem. Finally I noticed that the ClassNotFoundExceptions were not in line with my code, they were coming from wrong (old) packages. I checked if there was some other plugin which was messing with my debugs/exports and indeed there was, my own plugin!
So a simple fix to try if you're facing this and the CNFE's are not in line with your code:
Go to "Install new software"
Click on "already installed"
Remove all instances of your package/plugin and restart
Likely this was caused because I changed the plugin ID, making Eclipse treat it as a new plugin.
Another good site to take a look if you're getting frustrated and stuck: http://www.eclipsezone.com/eclipse/forums/t99010.html
In my case there was this Message "Activator ..invalid" but in the next exceptions there were ClassNotFound Exceptions in a Bundle were i didnt change something..
Guu(Posted a solution too) is my hero, After increasing
Bundle-ManifestVersion: 2
to
Bundle-ManifestVersion: 3
everything works :)
I got the same exception. The underlying problem was a ClassCastException. My bundle requires org.osgi.core 4.3 whereas the equinox launcher uses 4.2.
Regards
Roland
This can also happen if you name a bundle after a package in another bundle.
So:
if you have Bundle A which contains package org.my.package.name.function,
and you create bundle B with name org.my.package.name.function,
=> then the system may look for the activator there, and not find any.
I found the reason of the error. The error occurs when i try create a new object from any other class in the constructor of activator class. Isn't it legal to create an object in activator of plugin ?
In my case this exception was because of inability of Eclipse custom class loader to resolve and load all depending classes from other plugins in-time. I am not Eclipse super-guru so maybe it was my fault.
However it was fixed by disabling lazy loading of plugin. In GUI on Overview tab of MANIFEST.MF editor uncheck Activate this plug-in when one of its classes is loaded. Or directly in MANIFEST.MF delete line
Bundle-ActivationPolicy: lazy
Another captain obvious: If you change the paths of your source files (e.g. src/ to src/main/java), but forget to update build.properties, the compilation will always succeed, but your plugin will never work.
I had the same error, in my case I created my own constructor with parameters. But I didn't provide a default constructor. So after removing my constructor and initialized all within the start() method, it worked like charme.
I also met the same error. The activator XX for bundle XX is invalid, and the ClassNotFoundException.
I checked plugins\ directory, and could not find the class needed.
--
Because there is no jar file containing the needed class, there is only the corresponding directory.
For example, there is no com.hh.jar, but only com.hh directory.
So, there must be something wrong about creating the com.hh.jar.
if com.hh.jar reference other plugins, then also check them.
I solved the problem by editing MANIFEST.MF.
Open it by Plug-in Manifest Editor, in runtime tab, add needed packages in "Exported Packages".
and in the "classpath", add needed libraries, and, "." (current directory, IMPORTANT)
I have also run into this isue when 'bundle-izing' plain jar files. If some dependencies are not resolved, or jars depend on a higher JAVA version than the one you're using, the activator will not start, giving the above exception. The quick way to find out if this is the problem is to remove the jars from the bundle-classpath (runtime tab of the manifest) and check if the activator will run correctly.

Categories

Resources