Wha's wrong with these codes?

Im writing an action for intellj, just wanna add a library to global library table.
Codes are here:
public class AddLibraryAction extends AnAction {
public void actionPerformed(AnActionEvent e) {
final LibraryTable globalTable = LibraryTablesRegistrar.getInstance().getLibraryTable();

ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
LibraryTable.ModifiableModel tableModel = globalTable.getModifiableModel();

Library newLib = tableModel.createLibrary("TestingLib");
Library.ModifiableModel libModel = newLib.getModifiableModel();

String localJarPath = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, "D:/PluginProblem/junit-4.1.jar");
VirtualFile vFile = VirtualFileManager.getInstance().findFileByUrl(localJarPath);
if (vFile != null) {
libModel.addRoot(vFile, OrderRootType.CLASSES);


The jar is absolutely existing and the library is created in the global library list.
But I add this library to a module dependency. I still can't find the classes in the junit.jar.

The library showed in the project view is empty, no jar items can be expanded.
If I try to attach the junit.jar again in the new library, intelliJ will process a parsing command.

How can I invoke the parsing process after I add a new library?
Anything wrong with my codes?

1 comment
Comment actions Permalink

I know what's wrong about this.

I should use the jar protocal to locate the jar file, not the local file system protocal:
String path = "jar://D:/PluginProblem/junit-4.1.jar!/";

Use this path to get the VirtualFile and add it to library table.

Then the contents can be scanned.


Please sign in to leave a comment.