Intellij14: How to get the classpath of the current project?

Hi,

  I am developing a plugin for IntelliJ. I need to determine in a string the full classpath of the current project, including compiled sources and all third-party libraries. Searching the forum, this question was already asked many years ago:

https://devnet.jetbrains.com/message/502975#502975

Project project = (Project) event.getDataContext().getData(DataConstants.PROJECT);
ProjectRootManager projectManager = ProjectRootManager.getInstance(project);
projectManager.getFullClassPath();


However, it looks like it relies on API that does not exist anymore. By what was getFullClassPath() replaced in IntelliJ 14?


Andrea

0
11 comments

"The classpath of the project" is not a very meaningful thing in IntelliJ IDEA because each module has its own independent classpath. You can combine the classpaths of all modules in the project, and that's what that method did, but trying to use that classpath is unlikely to result in correct behavior in multi-module projects.

The modern API to get the classpath of a module is the OrderEnumerator class. For example, you can call it as OrderEnumerator.orderEntries(module).recursively().getClassesRoots().

0

Thanks Dmitry.  OrderEnumerator.orderEntries(module).recursively().getClassesRoots() does seem to work, but it does not seem to include the output directory of the current module for its compiled classes. How to get that folder's absolute path? I searched the documentation, and I did not find a solution, apart from this very same question already asked in the past, but with no answer:

https://devnet.jetbrains.com/message/5222469#5222469

A workaround is to check if either "out/production/<module_name>" or "target/classes" (for Maven projects) exists. But it does not sound like a robust solution (eg if project is configured with a custom output directory).

thanks

Andrea

0

See com.intellij.openapi.compiler.CompilerPaths#getModuleOutputPath


0

Based on the other comments, what it seems to work for me now is the following (posted here in case other people will have the same need):

public static String getFullClassPath(Module m){
    String cp = "";
    cp += CompilerPaths.getModuleOutputPath(m,false);

    for(VirtualFile vf : OrderEnumerator.orderEntries(m).recursively().getClassesRoots()){
        String entry = new File(vf.getPath()).getAbsolutePath();
        if(entry.endsWith("!/")){ //not sure why it happens in the returned paths
            
entry = entry.substring(0,entry.length()-2);
        }
        if(entry.endsWith("!")){
            entry = entry.substring(0,entry.length()-1);
        }
        cp += File.pathSeparator + entry;
    }
    return cp;
}
0
Avatar
Permanently deleted user

A simple way to do this would be :

String fullClasspath = OrderEnumerator.orderEntries(module).recursively().getPathsList().getPathsString()
0

there is no clue here how we can have module?  I find it hard when running a subprocess from plugin which requires access to classpath.

0

anonymous It is unclear what your goal/environment is. See also https://plugins.jetbrains.com/docs/intellij/module.html reference.

0

Thanks Yann Cebron

0

another different question I do not see any logs from plugin when it installed with IDE.  I see all possible options bridging with log4j over slf4j as dependencies can't be changed  to Intellij Logger or slf. basically plugin triggers a process builder.

I am surprised , in dev mode runIDE clearly showing console log from dependencies now after installing plugin all logs missing with no clue for user on what is happening :(. Hope IJ have clear solution for this.

0

anonymous See https://plugins.jetbrains.com/docs/intellij/ide-infrastructure.html#logging on correct use of logging. Please do open new thread when you have unrelated questions, thanks.

0

Apologies and thank you

0

Please sign in to leave a comment.