-1

I have tried many methods over the last few days with no success...I've trawled through various Stackoverflow entries and to no avail...I must be missing something.

I have tried across three different IDEs...IntelliJ, Eclispe and Netbeans.

The problem is when trying to turn my program into an executable jar it is unable to run (either by double clicking or running through command).

When executing the following on command:

java -jar D:\Computing\Programming\Java\Projects\JavaFXGameMenu\out\artifacts\JavaFXGameMenu_jar\JavaFXGameMenu.jar

I get: Error: Could not find or load main class root.Main

When i run the same but with javaw instead.. i get not error message, but nothing happens either.

I am predominately using IntelliJ as its a JavaFX application that I am building.

This is the project hierarchy: enter image description here

When creating the Artifact I choose the following based upon other threads:

enter image description here

I then re run this using: Java -jar D:\Computing\Executables\JavaFXGameMenu.jar

I get the following issue:

enter image description here

I have put the relevant environment variables into my system and Path and I am using jre1.8.0_144.

Any help with tracking down what the problem could be is greatly appreciated

Code below...but this fully compiles and runs within IDE without errors...the problem is when its turned into a .jar and ran from command.

package root;

import javafx.application.Application;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;


public class Main extends Application {

    private double width = 1920;
    private double height = 1080;

    private Parent createContent(){
        Pane root = new Pane();
        root.setPrefSize(width, height); //W:860 H:600
        ImageView imgLogo = null;
        ImageView bottomlogo = null;
        MenuItem newGame = new MenuItem("NEW GAME");
        MenuItem continueGame = new MenuItem("CONTINUE");
        MenuItem friends = new MenuItem("FRIENDS");
        MenuItem settings = new MenuItem("SETTINGS");
        MenuItem store = new MenuItem("STORE");
        MenuItem exit = new MenuItem("EXIT");

        try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/dark.jpg"))) {
            ImageView img = new ImageView(new Image(is));
            img.setFitWidth(width);
            img.setFitHeight(height);
            root.getChildren().add(img);
        } catch (IOException e){
            System.out.println("Couldn't Load Image");
        }

        try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/logo.png"))) {
            imgLogo = new ImageView(new Image(is));
            imgLogo.setX(1000);
            imgLogo.setY(100);
            imgLogo.setFitWidth(600);
            imgLogo.setFitHeight(300);
        } catch (IOException e){
            System.out.println("Couldn't Load Image");
        }

        try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/SteamAgony.png"))) {
            bottomlogo = new ImageView(new Image(is));
            bottomlogo.setX(100);
            bottomlogo.setY(800);
            bottomlogo.setFitHeight(200);
            bottomlogo.setFitWidth(200);
            bottomlogo.setOpacity(0.7);
        } catch (IOException e){
            System.out.println("Couldn't Load Image");
        }

        MenuBox menu = new MenuBox(
                newGame,
                continueGame,
                friends,
                settings,
                store,
                exit);

        menu.setTranslateX(width / 3.4);
        menu.setTranslateY(height / 2.5);

        settings.setOnMouseClicked(event -> new SceneCreator().createScene(200,300));

        exit.setOnMouseClicked( event -> Platform.exit());

        root.getChildren().addAll(menu, imgLogo, bottomlogo);

        return root;
    }

    @Override
    public void start(Stage primaryStage) throws Exception{
        Scene scene = new Scene(createContent());
        primaryStage.setScene(scene);
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.show();
    }

    @Override
    public void stop(){
        //TODO
    }

    public static void main(String[] args) {
        launch(args);
    }
}
ArcherGilly
  • 149
  • 11
  • Possible duplicate of [What does "Could not find or load main class" mean?](https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean) – Ravi Sep 17 '17 at 10:31
  • Could you include the code that is adding null element – jrtapsell Sep 17 '17 at 10:39
  • @Ravi nowhere near duplicate of that answer. That is one of the threads I tried and results in the same issue. – ArcherGilly Sep 17 '17 at 10:53
  • @jrtapsell I have included the code above, but the line 102 refers to this segment and checking in IDE debug reveals it resolves as normal. root.getChildren().addAll(menu, imgLogo, bottomlogo); – ArcherGilly Sep 17 '17 at 10:58
  • 1
    The path is not valid when running from a JAR. See this question: https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar or possibly this one: https://stackoverflow.com/questions/403256/how-do-i-read-a-resource-file-from-a-java-jar-file – Itai Sep 17 '17 at 10:58
  • @sillyfly Thank you very much...the problem was I had to put absolute references into my Inputstreams in order to run outside of IDE. I amended and it works perfectly – ArcherGilly Sep 17 '17 at 11:05
  • @ArcherGilly: As suggested by sillyfly, why not use an [tag:embedded-resource], described [here](https://stackoverflow.com/tags/embedded-resource/info). – trashgod Sep 17 '17 at 11:23

4 Answers4

0

The error is at Main.createContent line 102. Maybe you forgot to initialize the child node (I'm not really familiar with JavaFX).

Cedric
  • 532
  • 5
  • 7
0

You are adding a null element as a child of a Pane, causing the null pointer issue you are facing, if you use the debugger you'll be able to find where the variable that shouldn't be null is being set to null.

Edit - after code added

You are seeing 2 fields in try catches (imageLogo, bottomLogo), and adding them even if they fail, which adds nulls, causing the error.

You use a relative path for the images, this is probably the issue, are you running the jar from the project root? If not you could put the absolute path, but using resources would be more reliable, and allow the jar to run on different computers.

Community
  • 1
  • 1
jrtapsell
  • 6,719
  • 1
  • 26
  • 49
  • It fully compiles within IDE and there is no Null Element within IDE showing either..this error message occurs when turning into a .jar and running through Command. – ArcherGilly Sep 17 '17 at 10:41
  • Try attaching the debugger to it running from the Jar, and step through your code – jrtapsell Sep 17 '17 at 10:42
  • I am running the jar from project root...this has all been built using Intellijs jar packager. As you've stated even with a relative path this is not working, it only works with absolute paths...This is not ideal because if this is to be run on another computer, the absolute path will not work. – ArcherGilly Sep 17 '17 at 12:12
  • On another computer you would want to use resources instead, so the files are in the jar – jrtapsell Sep 17 '17 at 12:53
0

As @cedrikk mentioned, the problem is related to your code in Main.createContent.

You said the problem is when trying to run the jar as an executable - did you try to run it within the IDE? If not - you should try to, and while at it - debug it to help you find the problem. Just right click your Main class and choose debug.

Regarding running it with javaw, the reason you got no error messages is because javaw executes java programs without a console - where you would normally get error messages unless using some logging solution.

Kunda
  • 463
  • 3
  • 5
  • This runs perfectly in IDE without Errors. Did you not think I would have used debug...it only occurs outside of IDE. – ArcherGilly Sep 17 '17 at 10:51
0

The problem was down to declaring inputStreams.

This works for both in IDE and running from jar:

    String bgImg = "root/resources/dark.jpg";
    URL bgImgPath = Main.class.getClassLoader().getResource(bgImg);
    ImageView img = new ImageView(new Image(bgImgPath.toExternalForm()));
    img.setFitWidth(width);
    img.setFitHeight(height);
    root.getChildren().add(img);

Compared to what I had before:

try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/dark.jpg"))){
    ImageView img = new ImageView(new Image(is));
    img.setFitWidth(width);
    img.setFitHeight(height);
    root.getChildren().add(img);
}catch (IOException e) {
    System.out.println("Could not load");
}

The changes to the paths, were from where I tried adding the resource folder to the root folder instead of within src.

ArcherGilly
  • 149
  • 11