JAR 文件无法运行



我最近用Maven和JavaFX写了一个简单的计算器,没什么特别的。代码运行没有问题,所有操作都没有问题——我试着编译jar文件,我编译了它,但是当我试着运行它时,什么都没有发生,这个进程甚至没有出现在管理器中。

我试图通过控制台运行,如果版本匹配JDK和JRE是相同的,我附加了屏幕。在启动时,它给出了这样一个错误,我试图修复它,谷歌它,尝试一切,没有问题连接JavaFX和没有。然后它排除了一个无法找到主类的错误,它还排除了一个关于清单文件的未知位置的错误。我纠正了所有这些错误,做了所有要求我做的事情——结果,什么都没有。一个又一个错误,一个又一个,第三个,然后又跳出来一些东西,最后,我什么都不懂。

打开jar文件

version JDK jre

主类-应用程序

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文件

<?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>

控制器

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文件

<?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>

为JavaFX程序创建jar文件并不是推荐的打包实践,因为它们存在几个问题。看看这里https://stackoverflow.com/tags/javafx/info,标题为"包装"。

最新更新