简体   繁体   English

JAR 文件不会运行

[英]JAR file won't run

I recently wrote a simple calculator using Maven and JavaFX, nothing special.我最近使用 Maven 和 JavaFX 编写了一个简单的计算器,没什么特别的。 The code runs without problems, all operations occur without problems - I try to compile the jar file, I compile it, but when I try to run it, nothing happens, the process does not even appear in the manager.代码运行没有问题,所有操作都没有问题 - 我尝试编译 jar 文件,我编译它,但是当我尝试运行它时,没有任何反应,该过程甚至没有出现在管理器中。

I tried to run through the console, if the versions match JDK and JRE are the same, I attached the screen.我尝试通过控制台运行,如果版本匹配JDK和JRE相同,我附上屏幕。 At startup, it gives such an error, I tried to fix it, googled it, tried everything, there were no problems with connecting JavaFX and there weren't.在启动时,它给出了这样的错误,我试图修复它,用谷歌搜索它,尝试了一切,连接 JavaFX 没有问题,但没有。 Then it knocks out an error that it cannot find the main class, it also knocked out an error about the unknown location of the manifest file.然后它敲出一个找不到主类的错误,它还敲出一个关于清单文件未知位置的错误。 I corrected all these mistakes and did everything that was required of me - as a result, absolutely nothing.我纠正了所有这些错误,并做了我需要做的一切——结果,一无所获。 After one mistake another, after another a third, then something else pops up and in the end, I don't understand anything.一个错误又一个,又一个三分之一,然后弹出另一个东西,最后,我什么都不懂。

open jar in cmd在cmd中打开jar

version jdk jre版本 jdk jre

main class - application主类——应用

package com.example.simplecalculator;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 322, 430);
        stage.setTitle("Калькулятор");
        stage.setScene(scene);
        stage.setResizable(false);
        stage.setAlwaysOnTop(true);
        stage.show();
    }

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

fxml file .xml 文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="436.0" prefWidth="322" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.simplecalculator.HelloController">
    <TextField fx:id="calculateField" editable="false" layoutX="32.0" layoutY="41.0" prefHeight="62.0" prefWidth="257.0" promptText="Введите число..." style="-fx-background-color: #DCDCDC;">
        <font>
            <Font size="27.0" />
        </font>
    </TextField>
    <Text fx:id="textMessage" fill="#6b6363" layoutX="33.0" layoutY="33.0" strokeType="OUTSIDE" strokeWidth="0.0" text="">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
    </Text>
    <Button fx:id="Button1" layoutX="36.0" layoutY="119.0" mnemonicParsing="false" onAction="#OneButtonClick" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="1" textFill="#595959">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor></Button>
    <Button layoutX="102.0" layoutY="119.0" mnemonicParsing="false" onAction="#SecondButtonClick" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="2" textFill="#595959">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor></Button>
    <Button layoutX="166.0" layoutY="118.0" mnemonicParsing="false" onAction="#ThirdButtonClick" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="3" textFill="#595959">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor></Button>
    <Button layoutX="36.0" layoutY="180.0" mnemonicParsing="false" onAction="#FourthButtonClick" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="4" textFill="#595959">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor></Button>
    <Button layoutX="102.0" layoutY="180.0" mnemonicParsing="false" onAction="#FifthButtonClick" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="5" textFill="#595959">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor></Button>
    <Button layoutX="166.0" layoutY="180.0" mnemonicParsing="false" onAction="#SixthButtonClick" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="6" textFill="#595959">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor></Button>
    <Button layoutX="37.0" layoutY="242.0" mnemonicParsing="false" onAction="#SeventhButtonClick" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="7" textFill="#595959">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor></Button>
    <Button layoutX="102.0" layoutY="242.0" mnemonicParsing="false" onAction="#EigthButtonClick" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="8" textFill="#595959">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor></Button>
    <Button layoutX="166.0" layoutY="242.0" mnemonicParsing="false" onAction="#NinthButtonClick" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="9" textFill="#595959">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor></Button>
    <Button layoutX="37.0" layoutY="303.0" mnemonicParsing="false" onAction="#ZeroButtonClick" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="0" textFill="#595959">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor></Button>
    <Button layoutX="102.0" layoutY="363.0" mnemonicParsing="false" onAction="#Result" prefHeight="46.0" prefWidth="182.0" style="-fx-background-color: #DCDCDC;" text="=" textFill="#595959">
        <font>
            <Font name="Arial" size="17.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor></Button>
    <Button layoutX="166.0" layoutY="303.0" mnemonicParsing="false" onAction="#CButtonClick" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="C" textFill="#595959">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor></Button>
    <Button layoutX="102.0" layoutY="303.0" mnemonicParsing="false" onAction="#deleteButton" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="←" textFill="#595959">
        <font>
            <Font name="Arial" size="17.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor>
    </Button>
    <Button layoutX="230.0" layoutY="180.0" mnemonicParsing="false" onAction="#minusButton" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="–" textFill="#595959">
        <font>
            <Font name="Arial Bold" size="17.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor>
    </Button>
    <Button layoutX="230.0" layoutY="242.0" mnemonicParsing="false" onAction="#multiplicationButton" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="*" textFill="#595959">
        <font>
            <Font name="Arial" size="17.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor>
    </Button>
    <Button layoutX="230.0" layoutY="303.0" mnemonicParsing="false" onAction="#segmentationButton" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="/" textFill="#595959">
        <font>
            <Font name="Arial" size="17.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor>
    </Button>
    <Button layoutX="230.0" layoutY="118.0" mnemonicParsing="false" onAction="#plusButton" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #DCDCDC;" text="+" textFill="#595959">
        <font>
            <Font name="Arial" size="17.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor>
    </Button>
    <Text fx:id="textMessage2" fill="#6b6363" layoutX="39.0" layoutY="19.0" strokeType="OUTSIDE" strokeWidth="0.0">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
    </Text>
    <Button layoutX="37.0" layoutY="363.0" mnemonicParsing="false" onAction="#helpButton" prefHeight="46.0" prefWidth="54.0" style="-fx-background-color: #f4f4f4;" text="Help!" textAlignment="RIGHT" textFill="#595959">
        <font>
            <Font name="Arial" size="12.0" />
        </font>
        <cursor>
            <Cursor fx:constant="HAND" />
        </cursor>
    </Button>
    <Text layoutX="137.0" layoutY="425.0" opacity="0.2" strokeType="OUTSIDE" strokeWidth="0.0" text="Версия 1.0.0" wrappingWidth="102.936767578125">
        <font>
            <Font size="9.0" />
        </font>
    </Text>
</AnchorPane>

controller控制器

package com.example.simplecalculator;

import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;

public class HelloController {

    public int num;
    public int result;
    static int calculation;

    @FXML
    TextField calculateField;
    @FXML
    Text textMessage;
    @FXML
    Text textMessage2;

    @FXML
    public void operation(){
        switch (calculation) {
            case 1 -> { // +
                result = num + Integer.parseInt(calculateField.getText());
                calculateField.setText(Integer.toString(result));
            }
            case 2 -> { // -
                result = num - Integer.parseInt(calculateField.getText());
                calculateField.setText(Integer.toString(result));
            }
            case 3 -> { // *
                result = num * Integer.parseInt(calculateField.getText());
                calculateField.setText(Integer.toString(result));
            }
            case 4 -> { // /
                result = num / Integer.parseInt(calculateField.getText());
                calculateField.setText(Integer.toString(result));
            }
        }
    }

    @FXML
    public void ClearMethod(){
        calculateField.clear();
    }

    public void initialize(){
        calculateField.setOnKeyTyped(Event::consume);
        calculateField.setOnKeyPressed(Event::consume);
    }

    @FXML
    public void pNumbers(){
        String check = calculateField.getText();
        if(check.length() >=  11){
            textMessage.setText("Слишком большое число!");
            return;
        }
        textMessage.setText("Результат:");
    }

    @FXML
    protected void OneButtonClick() {
        calculateField.setText(calculateField.getText() + "1");
    }

    @FXML
    protected void SecondButtonClick() {
        calculateField.setText(calculateField.getText() + "2");
    }

    @FXML
    protected void ThirdButtonClick() {
        calculateField.setText(calculateField.getText() + "3");
    }

    @FXML
    protected void FourthButtonClick() {
        calculateField.setText(calculateField.getText() + "4");

    }

    @FXML
    protected void FifthButtonClick() {
        calculateField.setText(calculateField.getText() + "5");

    }

    @FXML
    protected void SixthButtonClick() {
        calculateField.setText(calculateField.getText() + "6");

    }

    @FXML
    protected void SeventhButtonClick() {
        calculateField.setText(calculateField.getText() + "7");

    }

    @FXML
    protected void EigthButtonClick() {
        calculateField.setText(calculateField.getText() + "8");

    }

    @FXML
    protected void NinthButtonClick() {
        calculateField.setText(calculateField.getText() + "9");

    }

    @FXML
    protected void ZeroButtonClick() {
        calculateField.setText(calculateField.getText() + "0");

    }

    @FXML
    protected void CButtonClick() {
        ClearMethod();
        textMessage.setText("");
        textMessage.setVisible(true);
        textMessage2.setVisible(false);
    }

    @FXML
    protected void deleteButton() {
        String value = calculateField.getText();
        int length = value.length();

        if(length > 0){
            StringBuilder builder = new StringBuilder(value);
            builder.deleteCharAt(length-1);
            calculateField.setText(builder.toString());
        }
    }

    @FXML
    protected void plusButton() {
        pNumbers();
        try {
            num = Integer.parseInt(calculateField.getText());
            calculation = 1;
            calculateField.setText("");
            textMessage.setText(num + " +");
        } catch(NumberFormatException e){
            textMessage.setVisible(false);
            textMessage2.setText("Макс. значение для ввода равно 2,147,483,647! \n Измените число!");
            return;
        }
    }

    @FXML
    protected void minusButton(){
        pNumbers();
        try {
            num = Integer.parseInt(calculateField.getText());
            calculation = 2;
            calculateField.setText("");
            textMessage.setText(num + " -");
        } catch(NumberFormatException e){
            textMessage.setVisible(false);
            textMessage2.setText("Макс. значение для ввода равно 2,147,483,647! \n Измените число!");
            return;
        }
    }

    @FXML
    protected void multiplicationButton(){
        pNumbers();
        try {
            num = Integer.parseInt(calculateField.getText());
            calculation = 3;
            calculateField.setText("");
            textMessage.setText(num + " *");
        } catch(NumberFormatException e){
            textMessage.setVisible(false);
            textMessage2.setText("Макс. значение для ввода равно 2,147,483,647! \n Измените число!");
            return;
        }
    }

    @FXML
    protected void segmentationButton() {
        pNumbers();
        try {
            num = Integer.parseInt(calculateField.getText());
            calculation = 4;
            calculateField.setText("");
            textMessage.setText(num + " /");
        } catch(NumberFormatException e){
            textMessage.setVisible(false);
            textMessage2.setText("Макс. значение для ввода равно 2,147,483,647! \n Измените число!");
            return;
        }
    }

    @FXML
    protected void helpButton() {
        pNumbers();
    }

    @FXML
    protected void Result(){
        operation();
        textMessage.setText("Результат: ");
    }
}

xml-file xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>SimpleCalculator</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>SimpleCalculator</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.8.2</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>18</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>18</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <source>18</source>
                    <target>18</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running with: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>com.example.simplecalculator/com.example.simplecalculator.HelloApplication
                            </mainClass>
                            <launcher>app</launcher>
                            <jlinkZipName>app</jlinkZipName>
                            <jlinkImageName>app</jlinkImageName>
                            <noManPages>true</noManPages>
                            <stripDebug>true</stripDebug>
                            <noHeaderFiles>true</noHeaderFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Creating jar files for JavaFX programs is not the reccomended packaging practice anyway due to several issues with them.无论如何,为 JavaFX 程序创建 jar 文件并不是推荐的打包做法,因为它们存在一些问题。 Have a look here https://stackoverflow.com/tags/javafx/info under the headline "packaging".在标题“包装”下查看https://stackoverflow.com/tags/javafx/info

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM