4

I'm trying to develop a JavaFX application. I've to create a plugin system. My application is composed with a GridPane with 4 places where I want to insert 4 AnchorPanes.

I want that this AnchorPanes to be plugins. I've got the following architecture :

My Application
  + src
       + controller
       + view
       + Main.java
  + res
  + lib
  + plugin
       + plugin.jar
           + plugin
               + Controller.java
               + view.fxml

I want to call dynamically my plugin with a loader.

FXMLLoader loader = new FXMLLoader();
loader.setLocation(new URL(path));
pane = loader.load();

If I don't touch to anything, I've got the following error :

java.lang.ClassNotFoundException: plugin.Controller

If I right-click on my .jar and I select "Add as library" in my IDE, it works perfectly. But, I want to add my plugins dynamically so I must not do this manipulation. I think it's a problem of ClassPath but i don't know how to resolve it.

Vincent Rossignol
  • 215
  • 1
  • 2
  • 8
  • What's the value of `path`? If it is a relative path, what's your current working directory? Is `loader` a ClassLoader? If so which one? – Thomas Jul 21 '16 at 08:50
  • The value of path is the correct path to my fxml. It's not a relative path. Well, the loading of the fxml file works but not his associated controller. loader is a FXMLLoader. – Vincent Rossignol Jul 21 '16 at 08:54
  • If you want to dynamically add jars to your application (aka plugins) you need to either put them onto the classpath when starting the application (e.g. adding something like `plugin/*` to your classpath) or use a `ClassLoader` to load the plugin jar at runtime. – Thomas Jul 21 '16 at 09:01
  • It's what i expected. How i add plugin/* to my classpath ? – Vincent Rossignol Jul 21 '16 at 09:03
  • Depends on how you start the application but the basic way would be `java -cp "plugin/*" Main` (if you have multiple entries in your classpath - which I suspect you do, e.g. all the classes in `controller` etc. - you'll have to add a delimited list the format of which depends on the OS). Alternatively build a jar and define the classpath in the jar's manifest. – Thomas Jul 21 '16 at 09:05
  • Ok, it works, thank's to this : http://stackoverflow.com/a/16742141/5876590 ! Thank you ! – Vincent Rossignol Jul 21 '16 at 09:14
  • Note that this seems to only work when you start the application from your IDE. If you want it to be independent of the IDE (which you should) you'd need to add the information to the application itself, either in a jar manifest or some run script. – Thomas Jul 21 '16 at 09:18

1 Answers1

0

it's better to load the plugin main class and main method then you can load the fxml file and it will be load fxml's controller too. at the end return the panes object (FXML NODE) to base app.

RayanFar
  • 539
  • 11
  • 28