Error-Write access is allowed from event dispatch thread only

Hi,

I am trying to load a jar file at when a new custom lanuage file is loaded. I used following code at constructor
of the class which extends fileviewprovider.

        ApplicationManager.getApplication().runWriteAction(

                new Runnable() {

                    public void run() {

      if(!libraryLoaded){

       try{
        path = "/home/amila/jagtest7/jsjarfile/jaggery.jar" + JarFileSystem.JAR_SEPARATOR; // hardcoded 4 now
        jarUrl = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, path);
        jarVirtualFile = VirtualFileManager.getInstance().findFileByUrl(jarUrl);
        project = manager.getProject();


        module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(virtualFile);
        rootManager = ModuleRootManager.getInstance(ModuleUtil.findModuleForFile(virtualFile, project));
        rootModel   = rootManager.getModifiableModel();



        Library myLibrary = rootModel.getModuleLibraryTable().createLibrary("JaggeryLibrary");
        Library.ModifiableModel libraryModel = myLibrary.getModifiableModel();
        libraryModel.addRoot(jarVirtualFile, OrderRootType.CLASSES);
        libraryModel.commit();


        rootModel.commit();
           libraryLoaded = true;
       }catch (Exception e){


           libraryLoaded = false;

       }


      }}});





But I get the foollowing exception. But the library is loaded correctly.

[  79388]  ERROR - plication.impl.ApplicationImpl - Write access is allowed from event dispatch thread only
Details: Current thread: Thread[ApplicationImpl pooled thread 2,4,Idea Thread Group] 567746494
Our dispatch thread:Thread[AWT-EventQueue-1 11.1.3#IU-117.798, eap:false,6,Idea Thread Group] 79191063
SystemEventQueueThread: Thread[AWT-EventQueue-1 11.1.3#IU-117.798, eap:false,6,Idea Thread Group] 79191063
java.lang.Throwable


How can I launch is code in event dispatch thread?


Thanks..!!

2 comments
Comment actions Permalink

Thanks a lot for your reply. It seems to work now. When I give

  ApplicationManager.getApplication().invokeLater(

                new Runnable() {

                    public void run() {


                        ApplicationManager.getApplication().runWriteAction(


                                new Runnable() {
                                    @Override
                                    public void run() {
                                        if (!libraryLoaded) {


                                            try {
                                                path = "/home/amila/jagtest7/jsjarfile/jaggery.jar" + JarFileSystem.JAR_SEPARATOR;
                                                jarUrl = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, path);
                                                jarVirtualFile = VirtualFileManager.getInstance().findFileByUrl(jarUrl);
                                                project = manager.getProject();


                                                module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(virtualFile);
                                                rootManager = ModuleRootManager.getInstance(ModuleUtil.findModuleForFile(virtualFile, project));
                                                rootModel = rootManager.getModifiableModel();



                                                Library myLibrary = rootModel.getModuleLibraryTable().createLibrary("JaggeryLibrary");
                                                Library.ModifiableModel libraryModel = myLibrary.getModifiableModel();
                                                libraryModel.addRoot(jarVirtualFile, OrderRootType.CLASSES);
                                                libraryModel.commit();


                                                rootModel.commit();
                                                libraryLoaded = true;
                                            } catch (Exception e) {


                                                libraryLoaded = false;

                                            }


                                        }
                                    }
                                }






                        );

                    }
                }


        );


Which seems to pass  to UI thread from background thread and write action being enabled after that.
But do not know this is the correct method. Anyway no exceptions are thrown and works fine.


Thanks..!!

3

Please sign in to leave a comment.