【问题标题】:Program will not run when turned into .jar变成.jar后程序不会运行
【发布时间】:2017-09-17 10:28:37
【问题描述】:

在过去的几天里,我尝试了很多方法,但都没有成功……我浏览了各种 Stackoverflow 条目,但无济于事……我一定遗漏了一些东西。

我尝试过三种不同的 IDE...IntelliJ、Eclispe 和 Netbeans。

问题是当我试图将我的程序变成可执行 jar 时,它无法运行(通过双击或通过命令运行)。

在执行以下命令时:

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

我得到:错误:无法找到或加载主类 root.Main

当我运行相同但使用 javaw 时.. 我没有收到错误消息,但也没有任何反应。

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

这是项目层次结构:

在创建 Artifact 时,我会根据其他线程选择以下内容:

然后我重新运行它:Java -jar D:\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);
    }
}

【问题讨论】:

标签: java javafx executable-jar


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 这在 IDE 中完美运行,没有错误。你不认为我会使用调试...它只发生在 IDE 之外。
    【解决方案3】:

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

    编辑 - 添加代码后

    您在尝试捕获(imageLogo、bottomLogo)中看到 2 个字段,即使它们失败也添加它们,这会添加空值,从而导致错误。

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

    【讨论】:

    • 它在 IDE 中完全编译,IDE 中也没有显示 Null 元素..转换为 .jar 并通过命令运行时会出现此错误消息。
    • 尝试将调试器附加到从 Jar 运行的调试器上,并逐步执行您的代码
    • 我正在从项目根目录运行 jar...这一切都是使用 Intellijs jar 打包器构建的。正如您所说,即使使用相对路径也不起作用,它仅适用于绝对路径......这并不理想,因为如果要在另一台计算机上运行,​​绝对路径将不起作用。
    • 在另一台计算机上,您希望使用资源,因此文件在 jar 中
    【解决方案4】:

    问题在于声明 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 中的位置。

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2017-06-30
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多