Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » e(fx)clipse » ClassNotFoundException when loading Controller via FXML
ClassNotFoundException when loading Controller via FXML [message #1099826] Mon, 02 September 2013 09:10 Go to next message
Daniel Zimmermann is currently offline Daniel ZimmermannFriend
Messages: 81
Registered: July 2009
Member
Hi,

I just stumbled across a tiny inconvenience: When I start building my current ap as a plain JavaFX application, I build the FXGraph and respectivly the FXML with the controller already specified within. This worked fine.

Now I migrated to the OSGi based approach and was confronted with the named Exception. Funny enough within the FXGraph, it was found (and it is there!). So when I change the approach from the FXGraph based approach
component Root controlledby my_package.app.ui.RootController

to the Java internal
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setResources(bundle);
RootController controller = new RootController();
fxmlLoader.setController(controller);
this.rootWindow = (BorderPane) fxmlLoader.load(this.getClass().getResource("layout/Root.fxml").openStream());

it works as intended.

This is weird and I currently have no idea at all, why this is happening. Do you?

Thanks and greetings,
Daniel
Re: ClassNotFoundException when loading Controller via FXML [message #1099929 is a reply to message #1099826] Mon, 02 September 2013 12:05 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
It's not wired but expected in OSGi. The problem is that when the the
FXMLLoader is in the javafx bundle whereas your controller is in your
custom bundle which means the classloader of the javafx bundle won't see
your RootController.

You can fix this by:
a) setting the correct classloader on the FXMLLoader instance

b) you use our DI-Support (org.eclipse.fx.ui.di) as seen in the
OSGi-Example

c) use the low-level support below DI-Support named OSGiFXMLLoader and
part of fx.osgi.util

Tom

On 02.09.13 11:10, Daniel Zimmermann wrote:
> Hi,
>
> I just stumbled across a tiny inconvenience: When I start building my
> current ap as a plain JavaFX application, I build the FXGraph and
> respectivly the FXML with the controller already specified within. This
> worked fine.
>
> Now I migrated to the OSGi based approach and was confronted with the
> named Exception. Funny enough within the FXGraph, it was found (and it
> is there!). So when I change the approach from the FXGraph based approach
>
> component Root controlledby my_package.app.ui.RootController
>
> to the Java internal
>
> FXMLLoader fxmlLoader = new FXMLLoader();
> fxmlLoader.setResources(bundle);
> RootController controller = new RootController();
> fxmlLoader.setController(controller);
> this.rootWindow = (BorderPane)
> fxmlLoader.load(this.getClass().getResource("layout/Root.fxml").openStream());
>
>
> it works as intended.
>
> This is weird and I currently have no idea at all, why this is
> happening. Do you?
>
> Thanks and greetings,
> Daniel
Re: ClassNotFoundException when loading Controller via FXML [message #1100036 is a reply to message #1099929] Mon, 02 September 2013 15:10 Go to previous messageGo to next message
Daniel Zimmermann is currently offline Daniel ZimmermannFriend
Messages: 81
Registered: July 2009
Member
Thanks for the tipps! I will do that, when I'm back from my short vacation!

Daniel
Re: ClassNotFoundException when loading Controller via FXML [message #1112999 is a reply to message #1099826] Fri, 20 September 2013 13:51 Go to previous messageGo to next message
Mar Ifla is currently offline Mar IflaFriend
Messages: 22
Registered: September 2013
Junior Member
I'm having a similar problem trying to run a JavaFX OSGi application.

My controller class my.fx.test.app.MyNodeController loads its fxml located in layout/MyNode.fxml just fine.

But when I include another custom node object named my.fx.test.app.MyButton (extending Button) into MyNode.fxml, I get a ClassNotFoundException for my.fx.test.app.MyButton.

I'm using
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getClassLoader().getResource("layout/MyNode.fxml"));
to instantiate the loader.

Any idea what's wrong?

[Updated on: Sun, 22 September 2013 07:46]

Report message to a moderator

Re: ClassNotFoundException when loading Controller via FXML [message #1113214 is a reply to message #1112999] Fri, 20 September 2013 20:21 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Did you read the answer I gave to Daniel?

The problem you have has to to with visibility! The FXMLLoader uses the
classloader associated with the javafx-bundle so it will never ever be
able to see your custom class.

Solution:
a) set current bundle class loader on the FXMLLoader
b) use our FXML support

Tom

On 20.09.13 15:51, Mar Ifla wrote:
> I'm having a similar problem trying to run a JavaFX OSGi application.
>
> My controller class my.package.MyNodeController loads its fxml located
> in layout/MyNode.fxml just fine.
> But when I include another custom node object named my.package.MyRegion
> (extending Region) into MyNode.fxml, I get a ClassNotFoundException for
> my.package.MyRegion.
>
> I'm usingFXMLLoader fxmlLoader = new
> FXMLLoader(getClass().getClassLoader().getResource("layout/MyNode.fxml"));to
> instantiate the loader.
>
> Any idea what's wrong?
Re: ClassNotFoundException when loading Controller via FXML [message #1114123 is a reply to message #1113214] Sun, 22 September 2013 07:41 Go to previous message
Mar Ifla is currently offline Mar IflaFriend
Messages: 22
Registered: September 2013
Junior Member
I've read it, but apparantly not carefully enough. Smile

My problem was, that I "passed" the correct ClassLoader in FXMLLoader contructor (to get URL resource), but I didn't give the ClassLoader to the instance itself.

It works now using FXMLLoader.setClassLoader(). Thanks.
Previous Topic:FXGraph and the use Control constants (e.g. USE_PREF_SIZE)
Next Topic:Exporting an OSGi Application Project
Goto Forum:
  


Current Time: Fri Apr 19 04:46:50 GMT 2024

Powered by FUDForum. Page generated in 0.02762 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top