Please help - how to attach correctly sources to an existing library?


I'm working on a plugin for IDEA. I try to add informations about sources to a library representing by a jar. (like "Attach sources" to a .jar that we can do manually)

Library libraryChoose = ... ;
Library.ModifiableModel libraryModel = libraryChoose.getModifiableModel();
VirtualFile vf = VirtualFileManager.getInstance().findFileByUrl("file://C:/srcdir");
if (vf != null) {
libraryModel.addRoot(vf, OrderRootType.SOURCES);

Then the last line is run. We see in the debug window that an new information about sources is added but this new directory is not use ti find sources.

Do I forget to refresh something or Is there a mistake?
Does It exist an other way to attach sources to a jar file programmatically?

Kind Regards,
Sophie Bardet

1 comment

I found a solution for my problem, so I post it for others which could have the same. I obtained the library from the LibraryTable. It had a ModifiableModel too, so you could commit it.

So :

LibraryTable.ModifiableModel tableModel = rootModel.getModuleLibraryTable().getModifiableModel();
Library library = tableModel.getLibraries()[0]; // for this example

final Library.ModifiableModel libraryModel = libraryChoose.getModifiableModel();
String pathURL = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, "C:/srcdir");
VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(pathURL);
libraryModel.addRoot(file, OrderRootType.SOURCES);

ApplicationManager.getApplication().runWriteAction(new Runnable(){
public void run() {

If you have some exceptions from the last commit from ModifiableRootManager, don't forget to do it in a write Action.

Sophie Bardet


Please sign in to leave a comment.