Java 当转换为.jar时,程序将不会运行

Java 当转换为.jar时,程序将不会运行,java,javafx,executable-jar,Java,Javafx,Executable Jar,在过去的几天里,我尝试了很多方法,但都没有成功…我拖网浏览了各种Stackoverflow条目,但没有成功…我一定是错过了什么 我已经尝试了三种不同的IDE…IntelliJ、Eclispe和Netbeans 问题是,当试图将我的程序转换为一个无法运行的可执行jar时(通过双击或通过命令运行) 执行以下on命令时: java-jar D:\Computing\Programming\java\Projects\JavaFXGameMenu\out\artifacts\JavaFXGameMenu

在过去的几天里,我尝试了很多方法,但都没有成功…我拖网浏览了各种Stackoverflow条目,但没有成功…我一定是错过了什么

我已经尝试了三种不同的IDE…IntelliJ、Eclispe和Netbeans

问题是,当试图将我的程序转换为一个无法运行的可执行jar时(通过双击或通过命令运行)

执行以下on命令时:

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

我得到:错误:无法找到或加载主类根。main

当我运行相同的但使用javaw的时候。。我没有收到错误消息,但也没有发生任何事情

我主要使用IntelliJ作为我正在构建的JavaFX应用程序

这是项目层次结构:

创建工件时,我根据其他线程选择以下选项:

然后,我使用Java-jard:\Computing\Executables\JavaFXGameMenu.jar重新运行它

我得到以下问题:

我已经将相关的环境变量放入我的系统和路径中,我正在使用jre1.8.0_144

非常感谢您能帮助我们找出问题所在

下面的代码…但这完全编译并在IDE中运行,没有错误…问题是当它变成.jar并从命令运行时

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);
    }
}

错误位于Main.createContent第102行。可能您忘记初始化子节点(我对JavaFX不太熟悉)。

您添加了一个null元素作为窗格的子元素,这导致了您面临的null指针问题,如果使用调试器,您将能够找到不应该为null的变量被设置为null的位置

编辑-添加代码后 您在try-catch中看到了两个字段(imageLogo、bottomLogo),即使它们失败也会添加它们,这会添加null,从而导致错误


您使用图像的相对路径,这可能是问题所在,您是否从项目根运行jar?如果不是,您可以放置绝对路径,但是使用资源会更可靠,并允许jar在不同的计算机上运行。

正如@cedrikk提到的,问题与Main.createContent中的代码有关

您说过问题是在尝试将jar作为可执行文件运行时- 您是否尝试在IDE中运行它?如果没有-你应该尝试,并在它-调试它,以帮助你找到问题。只需右键单击主类并选择debug


关于使用javaw运行它,没有错误消息的原因是javaw在没有控制台的情况下执行java程序,除非使用某种日志解决方案,否则通常会在控制台上收到错误消息。

问题在于声明inputStreams

这既适用于IDE,也适用于从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);
与之前相比:

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");
}

对路径的更改是从我尝试将资源文件夹添加到根文件夹而不是src中的位置开始的

您是否可以包含添加null的代码element@Ravi与这个答案相差甚远。这是我尝试过的线程之一,并导致了相同的问题。@jrtapsell我已经包含了上面的代码,但第102行引用了此段,签入IDE debug显示它正常解析。root.getChildren().addAll(菜单、imgLogo、底部徽标);从JAR运行时,路径无效。请看这个问题:或者可能是这个问题:它在IDE中完全编译,IDE中也没有显示任何空元素。。当转换为.jar并运行through命令时,会出现此错误消息。请尝试将调试器附加到从jar运行的调试器上,我正在从项目根目录运行jar…这一切都是使用Intellijs jar packager构建的。正如您所说的,即使使用相对路径,这也不起作用,它只适用于绝对路径……这并不理想,因为如果要在另一台计算机上运行,绝对路径将不起作用。在另一台计算机上,您可能希望使用资源,因此文件在JAR中。这在IDE中运行完全没有错误。你们没想到我会使用debug…它只发生在IDE之外。