0

I have my own library, it contains one module named goodapp. The module has one class named App inside goodapp.base package.

The module info looks like this:

module goodapp.base {
    requires transitive javafx.graphics;
    exports goodapp.base;
}

The App class:

import javafx.application.Application;
import javafx.stage.Stage;

public abstract class App extends Application {

    //------Public

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public abstract void init() throws Exception;

    @Override
    public abstract void start(Stage primaryStage) throws Exception;

    @Override
    public abstract void stop() throws Exception;

}

I built the jar using Intellij artifact system, it's configured that way:

enter image description here

AND...

I created another project with a module named tester.main. I added goodapp as dependency, subclassed the App class and when I try to run the project I get the exception:

Exception in Application constructor java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071) 
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class goodapp.base.App
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:890)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:831) 
Caused by: java.lang.InstantiationException
at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:802)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more Exception running application tester.main.AppImpl

I tried to bundle JavaFX in the JAR as well as just not bundling and linking it as a dependency to tester.main module.

Any ideas?

jewelsea
  • 150,031
  • 14
  • 366
  • 406
Wiktor
  • 89
  • 6
  • When posting an exception, always post the full stack trace (e.g. like this similar question: [Unable to construct javafx.application.Application instance](https://stackoverflow.com/questions/25599423/unable-to-construct-javafx-application-application-instance)). – jewelsea Aug 19 '21 at 22:41
  • The class it can't create is `goodapp.base.app`, but you have named your class `App` -> something is incorrect here regarding capitalization. – jewelsea Aug 19 '21 at 22:43
  • Your `App` class is `abstract`. It is not possible to construct a concrete instance of an abstract class. – jewelsea Aug 19 '21 at 22:44
  • There may be other issues in the code you don't show. – jewelsea Aug 19 '21 at 22:45
  • I don't recommend relying on the IntelliJ artifact system to build distributable jars for JavaFX applications, instead, use a 3rd party build system like Maven or Gradle in conjunction with appropriate JavaFX plugins, based on the "runtime images" info at [openjfx.io](https://openjfx.io/openjfx-docs/) and the [info supplied here](https://stackoverflow.com/a/68823040/1155209). – jewelsea Aug 19 '21 at 22:47
  • Consider if your application really warrants adding the additional complexity of a multi-module setup. – jewelsea Aug 19 '21 at 22:51
  • @jewelsea I already subclassed the class, take a look at the full stacktrace I posted. AppImpl is the subclass – Wiktor Aug 20 '21 at 13:04
  • You can't be helped with code you don't provide (e.g. a [mcve]). I am guessing there is some issue with AppImpl which is preventing JavaFX from instantiating. Perhaps it doesn't have public no-arg constructor, I don't know. Perhaps it is due to the subclassing (not using a direct subclass of Application). Perhaps it is because the launch method you are using is working on the class with the static main (App, not AppImpl). Perhaps it a module access issue. I advise starting with a hello world app without modules, then adding bits in (e.g. subclass, then modules) to find the break. – jewelsea Aug 23 '21 at 04:10
  • Reasons for a runtime exception form launch (from javadoc): "if there is an error launching the JavaFX runtime, or if the application class cannot be constructed (e.g., if the class is not public or is not in an exported package), or if an Exception or Error is thrown by the Application constructor, init method, start method, or stop method." – jewelsea Aug 23 '21 at 04:12
  • The key error line in your stack trace is: `Caused by: java.lang.InstantiationException at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)` . So your issue is a access issue or the required constructor does not exist. – jewelsea Aug 23 '21 at 04:14

0 Answers0