How to add jar as module library?
I'm trying to add a jar as module library in following way:
ModifiableRootModel rootModel = ModuleRootManager.getInstance(getModule()).getModifiableModel();
LibraryTable.ModifiableModel modifiableModel = rootModel.getModuleLibraryTable().getModifiableModel();
VirtualFile libraryPath = VirtualFileManager.getInstance().findFileByUrl(jar);
if (libraryPath != null) {
Library iceLibrary = modifiableModel.createLibrary("Ice");
Library.ModifiableModel libModel = iceLibrary.getModifiableModel();
libModel.addRoot(libraryPath, OrderRootType.CLASSES);
libModel.commit();
modifiableModel.commit();
/* 115 */ rootModel.addLibraryEntry(iceLibrary);
rootModel.commit();
}
But got a NPE:
java.lang.NullPointerException
at com.intellij.openapi.roots.impl.LibraryOrderEntryImpl.getLibraryLevel(LibraryOrderEntryImpl.java:199)
at com.intellij.openapi.roots.impl.LibraryOrderEntryImpl.addListeners(LibraryOrderEntryImpl.java:210)
at com.intellij.openapi.roots.impl.LibraryOrderEntryImpl.<init>(LibraryOrderEntryImpl.java:59)
at com.intellij.openapi.roots.impl.RootModelImpl.addLibraryEntry(RootModelImpl.java:389)
at org.xblackcat.frozenice.facet.IceFacet$1$2.hyperlinkUpdate(IceFacet.java:115)
What's wrong with the code? What a proper way to add a module library to module?
请先登录再写评论。
The line 115 should simply be removed. The library is already added without it.
I've removed the line and got next exception (on line rootModel.commit(); line), but library was added to module.
[ 62341] ERROR - plication.impl.ApplicationImpl - Assertion failed: Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction())
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:98)
at com.intellij.openapi.application.impl.ApplicationImpl.assertWriteAccessAllowed(ApplicationImpl.java:1180)
at com.intellij.openapi.roots.impl.ProjectRootManagerImpl.fireBeforeRootsChanged(ProjectRootManagerImpl.java:471)
at com.intellij.openapi.roots.impl.ProjectRootManagerImpl.access$200(ProjectRootManagerImpl.java:73)
at com.intellij.openapi.roots.impl.ProjectRootManagerImpl$BatchSession.beforeRootsChanged(ProjectRootManagerImpl.java:139)
at com.intellij.openapi.roots.impl.ProjectRootManagerImpl$BatchSession.access$1700(ProjectRootManagerImpl.java:104)
at com.intellij.openapi.roots.impl.ProjectRootManagerImpl.makeRootsChange(ProjectRootManagerImpl.java:451)
at com.intellij.openapi.roots.impl.ModuleRootManagerImpl.makeRootsChange(ModuleRootManagerImpl.java:168)
at com.intellij.openapi.roots.impl.RootModelImpl.makeExternalChange(RootModelImpl.java:854)
at com.intellij.openapi.roots.impl.LibraryOrderEntryBaseImpl.updateFromRootProviderAndSubscribe(LibraryOrderEntryBaseImpl.java:116)
at com.intellij.openapi.roots.impl.LibraryOrderEntryBaseImpl.init(LibraryOrderEntryBaseImpl.java:48)
at com.intellij.openapi.roots.impl.ModuleLibraryOrderEntryImpl.doinit(ModuleLibraryOrderEntryImpl.java:71)
at com.intellij.openapi.roots.impl.ModuleLibraryOrderEntryImpl.<init>(ModuleLibraryOrderEntryImpl.java:49)
at com.intellij.openapi.roots.impl.ModuleLibraryOrderEntryImpl.cloneEntry(ModuleLibraryOrderEntryImpl.java:135)
at com.intellij.openapi.roots.impl.RootModelImpl.setOrderEntriesFrom(RootModelImpl.java:237)
at com.intellij.openapi.roots.impl.RootModelImpl.docommit(RootModelImpl.java:490)
at com.intellij.openapi.roots.impl.ModuleRootManagerImpl.doCommit(ModuleRootManagerImpl.java:216)
at com.intellij.openapi.roots.impl.ModuleRootManagerImpl.commitModelWithoutEvents(ModuleRootManagerImpl.java:212)
at com.intellij.openapi.roots.impl.ModuleRootManagerImpl.access$100(ModuleRootManagerImpl.java:65)
at com.intellij.openapi.roots.impl.ModuleRootManagerImpl$2.run(ModuleRootManagerImpl.java:233)
at com.intellij.openapi.module.impl.ModuleManagerImpl$8.run(ModuleManagerImpl.java:951)
at com.intellij.openapi.roots.impl.ProjectRootManagerImpl.makeRootsChange(ProjectRootManagerImpl.java:453)
at com.intellij.openapi.module.impl.ModuleManagerImpl.commitModel(ModuleManagerImpl.java:935)
at com.intellij.openapi.module.impl.ModuleManagerImpl.access$2000(ModuleManagerImpl.java:75)
at com.intellij.openapi.module.impl.ModuleManagerImpl$ModuleModelImpl.commitWithRunnable(ModuleManagerImpl.java:841)
at com.intellij.openapi.module.impl.ModuleManagerImpl.commitModelWithRunnable(ModuleManagerImpl.java:604)
at com.intellij.openapi.roots.impl.ModuleRootManagerImpl.multiCommit(ModuleRootManagerImpl.java:241)
at com.intellij.openapi.roots.impl.ModuleRootManagerImpl.commitModel(ModuleRootManagerImpl.java:208)
at com.intellij.openapi.roots.impl.RootModelImpl.commit(RootModelImpl.java:476)
at org.xblackcat.frozenice.facet.IceFacet$1$2.hyperlinkUpdate(IceFacet.java:115)
The text of the assertion tells you exactly what you need to do to fix it.
You are right!
Thanks a lot!