Caused by: java.lang.ClassNotFoundException: org.jetbrains.idea.maven.project.MavenProjectsManager PluginClassLoader
I am developing a IDEA plugin, I wrote a action class to get MavenProject(org.jetbrains.idea.maven.project.MavenProject) instance of project. here is the code:
package com.example.demo.actions;
import com.intellij.ide.IdeView;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import org.jetbrains.idea.maven.project.MavenProject;
import org.jetbrains.idea.maven.project.MavenProjectsManager;
public class ProjectViewPopupMenuTestAction extends AnAction {
@Override
public void actionPerformed(AnActionEvent e) {
IdeView ideView = e.getData(LangDataKeys.IDE_VIEW);
if (ideView == null) {
return;
}
PsiDirectory chooseDirectory = ideView.getOrChooseDirectory();
PsiFile psiFile = chooseDirectory.findFile("pom.xml");
VirtualFile virtualFile = psiFile.getVirtualFile();
MavenProject mavenProject = MavenProjectsManager.getInstance(e.getProject()).findProject(virtualFile);
System.out.println(mavenProject);
}
}
in plugin.xml, I add ProjectViewPopupMenuTestAction to ProjectViewPopupMenu:
<actions>
<action id="pomProjectViewPopup" class="com.example.demo.actions.ProjectViewPopupMenuTestAction"
text="getPomDependency" description="ProjectViewPopupMenu">
<add-to-group group-id="ProjectViewPopupMenu" anchor="last"/>
</action>
</actions>
I need maven plugin,so I added maven plugin in intellij block. here is my build.gradle.kts:
plugins {
id("java")
id("org.jetbrains.kotlin.jvm") version "1.7.20"
id("org.jetbrains.intellij") version "1.13.3"
}
group = "com.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
// Configure Gradle IntelliJ Plugin
// Read more: https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html
intellij {
version.set("2022.2.4")
type.set("IC") // Target IDE Platform
plugins.set(listOf("maven"))
}
tasks {
// Set the JVM compatibility versions
withType<JavaCompile> {
sourceCompatibility = "11"
targetCompatibility = "11"
}
withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = "11"
}
patchPluginXml {
sinceBuild.set("222")
untilBuild.set("232.*")
}
signPlugin {
certificateChain.set(System.getenv("CERTIFICATE_CHAIN"))
privateKey.set(System.getenv("PRIVATE_KEY"))
password.set(System.getenv("PRIVATE_KEY_PASSWORD"))
}
publishPlugin {
token.set(System.getenv("PUBLISH_TOKEN"))
}
}
Question:
when i debug the code, it threws ClassNotFoundException while run with:
MavenProject mavenProject = MavenProjectsManager.getInstance(e.getProject()).findProject(virtualFile);
then goto com.intellij.openapi.actionSystem.ex.ActionUtil:336:
AccessToken ignore = SlowOperations.allowSlowOperations(SlowOperations.ACTION_PERFORM)
and then goto 345, threw the NoClassDefFoundError:

but I stopped in breakpoint[VirtualFile virtualFile = psiFile.getVirtualFile();] and opened evaluate window to excute the rest code:

It works!!!
I am really confused by this exception over 5 days. please.
my environment: Development IDEA: IntelliJ IDEA 2023.1 Build #IU Mac OS X
plugin debug environment: 2023-04-27 11:27:07,938 [ 63374] SEVERE - #c.i.i.p.PluginManager - IntelliJ IDEA 2022.2.4 Build #IC-222.4459.24
2023-04-27 11:27:07,944 [ 63380] SEVERE - #c.i.i.p.PluginManager - JDK: 17.0.5; VM: OpenJDK 64-Bit Server VM; Vendor: JetBrains s.r.o.
2023-04-27 11:27:07,945 [ 63381] SEVERE - #c.i.i.p.PluginManager - OS: Mac OS X
I have looked for some same open-source plugin code, our code is similar, and google so many times, no result. please.
Please sign in to leave a comment.
Hi,
You probably miss the
<depends>org.jetbrains.idea.maven</depends>part in the plugin.xml file.See https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html#3-dependency-declaration-in-pluginxml
thx. it works.