fx:controller classNotFoundException plugin development

Hi guys,

I am developing a plugin, whose only Action launches a JavaFX application. This one loads a fxml file, where I indicate a controller by fx:controller="fxml.GUIController". This is causing error because of the controller and says is not finding the class of the controller. It is weird because if I run directly the javafx application, it loads perfectly the fxml and its controller, but when I run the plugin that launches the application, it does not work..I attach the files with the code!::

github link https://github.com/rafads7/TaskAnnotationPlugin

error:

 

-----Plugin Action:

Main:

fxml (just the important):

and the project structure

 

15 comments
Comment actions Permalink

Could you please attach your project? Retyping from the screenshots cause too many errors.

Thanks

0
Comment actions Permalink

Thank you for sharing the code!

The problem is probably caused by the default class loader in FXMLLoader. It's a static field and it's changed inside the SceneBuilder, that's why it doesn't see classes from your plugin. To fix this you need to set your plugin's class loader as a default for FXMLLoader, like the following:

FXMLLoader.setDefaultClassLoader(Thread.currentThread().getContextClassLoader())

 

0
Comment actions Permalink

thanks Pavel, but I don't understand where should I place this code, in the Plugin class? just before the launching of the application?

0
Comment actions Permalink

You need to set your plugin's class loader right before loading .fxml file, in your case it's MainPluginApplication.start().

I've tried it this way and it worked for me:

FXMLLoader loader = new FXMLLoader();
loader.setClassLoader(getClass().getClassLoader()); // set the plugin's class loader
loader.setLocation(getClass().getResource("/fxml/PluginFXML.fxml"));

It's a bit better than changing the default class loader because it doesn't interfere with the SceneBuilder

0
Comment actions Permalink

Thanks man!!! it works!!! ,, but then, in the class HTMLDiagramModifier.java, I cannot access to the route indicated in order to read the files. Either it says  java.io.FileNotFoundException: \html\dt.html (The system cannot found the specified route) or java.io.FileNotFoundException: \dt.html (Access denied)..

0
Comment actions Permalink

I cannot access to the files since I wrote the line you told me...I wrote it better here:: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000665130-java-io-FileNotFoundException-Access-is-denied-and-System-cannot-find-specified-route 

 

IF YOU CAN HELP ME PLEASE!! thaanks

0
Comment actions Permalink

Hi Rafael,

we don't advise to use JavaFX in IDEA's plugins as then you would meet all issues caused by JavaFX & Swing interop: like hanging IDEA instance, etc.

To the load problem: when you call getResource() it will get the resource in the output folder as JVM has no idea about your sources. You don't need to change file in output though, but need to recompile resources and the changes from sources would be propagated to the output.

You cannot read probably because you didn't close the writer and the file is locked.

Hope it helps,

Anna

0
Comment actions Permalink

Hi Anna,

thanks for answering! now I think is too late for me to start a new application withouth JavaFX, but next time I will take it into account! 

Even that, the thing is that there is no writer opened,, it does not allow me to read even on the first time, and just after trying to write (by try-catch), indeed, I think I close it... 

0
Comment actions Permalink

You try to read/write file in output directory, it is updated on make and there could be all kind of conflicts. Why do you need to do that? If your plugin would be installed in user's IDEA, do you expect that your plugin's jars would be modified? That must be prohibited due to e.g. security reasons.

0
Comment actions Permalink

Yes you are right..I was doing that because I don't know why my WebViewer object was loading the html from there,, and I don't know how to set the /src/... address, I have tried all the possible paths but none of them works, is this what I would like to know how to fix.. to enter a correct path into the WebViewer and also the PrintWriter for writing.

0
Comment actions Permalink

Why do you need to write to your source code from your app? 

0
Comment actions Permalink

the Webviewer needs to load a html file that draws a tree diagram, and its nodes content depends on the data the user inputs in the javafx application. As well as the user is entering new data, the diagram view should be updated.

0
Comment actions Permalink

This won't work, sorry. E.g. obviously if you publish your plugin, html file would be inside jar and you won't be able to write there without reloading of IDEA. 

I guess, you need to get nodes dynamically and not hardcoded into html, though it's already out of scope of writing IDEA's plugins.

0
Comment actions Permalink

Oh, okey I understand.. the thing was confusing me because when I was running the project from the main javafx application instead of from the plugin action, it was working and updating the html... But I understand what you mean.

Thank you very much for your help and time.

Last, if it is not too much for asking you to another advice, do you know what language would be then optimal or how I could do it in order to do what I described you and be able draw a tree diagram like this one? :: (don't take into account the blue boxes, just the tree diagram).

0

Please sign in to leave a comment.