JavaFX for Android Studio plugin

Answered

Hi,

I'm writing an IntelliJ IDEA plugin for Android Studio using JavaFX for the GUI. In my plugin I simply launch a JavaFX application (more precisely, a class Main that extends javafx.application) from the actionPerformed method of my plugin. If I run the program in IntelliJ IDEA everything works correctly but if I export the .jar file (by Build|Prepare plugin module for deployment) and run it in Android Studio I have this error:

java.lang.NoClassDefFoundError: javafx/application/Application

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:763)

at java.lang.ClassLoader.defineClass(ClassLoader.java:642)

at com.intellij.util.lang.UrlClassLoader._defineClass(UrlClassLoader.java:272)

at com.intellij.util.lang.UrlClassLoader.defineClass(UrlClassLoader.java:268)

at com.intellij.util.lang.UrlClassLoader._findClass(UrlClassLoader.java:237)

at com.intellij.ide.plugins.cl.PluginClassLoader.loadClassInsideSelf(PluginClassLoader.java:119)

at com.intellij.ide.plugins.cl.PluginClassLoader.tryLoadingClass(PluginClassLoader.java:73)

at com.intellij.ide.plugins.cl.PluginClassLoader.loadClass(PluginClassLoader.java:62)

at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

at template.TextBoxes.actionPerformed(TextBoxes.java:42)

at com.intellij.openapi.actionSystem.ex.ActionUtil$1.run(ActionUtil.java:197)

at com.intellij.openapi.application.TransactionGuardImpl.runSyncTransaction(TransactionGuardImpl.java:88)

at com.intellij.openapi.application.TransactionGuardImpl.submitTransactionAndWait(TransactionGuardImpl.java:156)

at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:211)

at com.intellij.openapi.actionSystem.impl.ActionButton.actionPerformed(ActionButton.java:189)

at com.intellij.openapi.actionSystem.impl.ActionButton.performAction(ActionButton.java:155)

at com.intellij.openapi.actionSystem.impl.ActionButton.processMouseEvent(ActionButton.java:326)

at java.awt.Component.processEvent(Component.java:6298)

at java.awt.Container.processEvent(Container.java:2236)

at java.awt.Component.dispatchEventImpl(Component.java:4889)

at java.awt.Container.dispatchEventImpl(Container.java:2294)

at java.awt.Component.dispatchEvent(Component.java:4711)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)

at java.awt.Container.dispatchEventImpl(Container.java:2280)

at java.awt.Window.dispatchEventImpl(Window.java:2746)

at java.awt.Component.dispatchEvent(Component.java:4711)

at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)

at java.awt.EventQueue.access$500(EventQueue.java:97)

at java.awt.EventQueue$3.run(EventQueue.java:709)

at java.awt.EventQueue$3.run(EventQueue.java:703)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)

at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)

at java.awt.EventQueue$4.run(EventQueue.java:731)

at java.awt.EventQueue$4.run(EventQueue.java:729)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)

at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:795)

at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:627)

at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:387)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Caused by: java.lang.ClassNotFoundException: javafx.application.Application PluginClassLoader[com.nicolas.locatelli.plugin.id, 1.0]

at com.intellij.ide.plugins.cl.PluginClassLoader.loadClass(PluginClassLoader.java:64)

at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
 
Although jfxrt.jar was present in my classpath I try to add this library as external (bad solution for portability), in this case, I have another error:
 
java.lang.RuntimeException: No toolkit found
at com.sun.javafx.tk.Toolkit.getToolkit(Toolkit.java:217)
at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:209)
at template.TextBoxes.actionPerformed(TextBoxes.java:43)
at com.intellij.openapi.actionSystem.ex.ActionUtil$1.run(ActionUtil.java:215)
at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:232)
at com.intellij.openapi.actionSystem.impl.ActionButton.actionPerformed(ActionButton.java:190)
at com.intellij.openapi.actionSystem.impl.ActionButton.performAction(ActionButton.java:156)
at com.intellij.openapi.actionSystem.impl.ActionButton.processMouseEvent(ActionButton.java:327)
at java.awt.Component.processEvent(Component.java:6306)
at java.awt.Container.processEvent(Container.java:2237)
at java.awt.Component.dispatchEventImpl(Component.java:4897)
at java.awt.Container.dispatchEventImpl(Container.java:2295)
at java.awt.Component.dispatchEvent(Component.java:4719)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)
at java.awt.Container.dispatchEventImpl(Container.java:2281)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4719)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:764)
at java.awt.EventQueue.access$500(EventQueue.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:715)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:737)
at java.awt.EventQueue$4.run(EventQueue.java:735)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:734)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:827)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:651)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:365)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
 
 
In order to launch javafx application from the plugin I use 
Application.launch(Main.class, (java.lang.String[])null);

or

PlatformImpl.startup(new Runnable() {
@Override
public void run() {
try {
Main main = new Main();
if (stage == null) {
stage = new Stage();
}
main.start(stage);
} catch (Exception e) {
e.printStackTrace();
}
}
});

I'm using Windows as OS, IntelliJ Idea 2017.1.3, Android Studio 3.0 and java jdk 1.8.0_121

I've read a lot of posts and discussions about similar problems but I couldn't find the answer. Do you know how can I fix it, please???

Thank you

1 comment
Comment actions Permalink

Make sure Android Studio runs on the Java Runtime which has JavaFX bundled.

As far as I know, AS 3.0 should include the new runtime with JavaFX, but previous AS versions didn't have it.

See https://intellij-support.jetbrains.com/hc/articles/206544879.

If JavaFX is not included with AS 3.0, users will have to manually change the runtime per the document linked above.

0

Please sign in to leave a comment.