0

I have searched, maybe this answer is similar to my question Eclipse - "Run as Java Application" when maven is involved, but I still cannot understand.

my project structure:

PackageTry
----src/main/java
--------ui
------------Init.java
----src/main/resources
--------css
------------Init.css
--------image
------------icon.png

package ui;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class Init extends Application{

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button button = new Button();
        button.setPrefSize(50, 50);
        button.setId("button");
        Group group = new Group();
        group.getChildren().add(button);
        Scene scene = new Scene(group,300,300);
         scene.getStylesheets().add(getClass().getResource("../main/resources/css/Init.css").toExternalForm());
        primaryStage.setScene(scene);
       primaryStage.show();
    }

}

When I choose run as->java application, it works well

but when I run the jar made by maven package, it shows that (Init:23 is scene.getStylesheets().add(getClass().getResource("../main/resources/css/Init.css").toExternalForm());)

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767).   
    Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at ui.Init.start(Init.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application ui.Init

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>PackageTry</groupId>
<artifactId>PackageTry</artifactId>
<version>0.0.1-SNAPSHOT</version>

<build>
  <sourceDirectory>src</sourceDirectory>
  <resources>
    <resource>
      <directory>src</directory>
      <excludes>
        <exclude>**/*.java</exclude>
      </excludes>
    </resource>
  </resources>
  <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
  <execution>
    <phase>package</phase>
    <goals>
      <goal>shade</goal>
    </goals>
    <configuration>
      <transformers>
        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
          <mainClass>ui.Init</mainClass>
        </transformer>
      </transformers>
    </configuration>
  </execution>
</executions>
 </plugin>
</plugins>
</build>

</project>

My Question: How to make the jar run normally. Thank you!

Community
  • 1
  • 1
YuLiu
  • 41
  • 1
  • 5
  • the maven shade plugin will only copy maven dependencies. You probably added the jfxrt.jar to your build path manually and so it isn't missing in eclipse. you can copy the dependency with the maven dependency plugin and try to alter the manifest in the shaded jar to include the copied dependency. Or maybe with the shade plugin you can add the jfxrt.jar hardcoded, idk - see http://stackoverflow.com/q/9294646/3858121 for reference – Japu_D_Cret Mar 25 '17 at 01:22
  • I changed scene.getStylesheets().add(getClass().getResource("../main/resources/css/Init.css").toExternalForm()); to scene.getStylesheets().add(getClass().getResource("/main/resources/css/Init.css").toExternalForm()); then it works – YuLiu Mar 25 '17 at 06:20

0 Answers0