Reputation: 11
I recently wrote a simple calculator using Maven and JavaFX, nothing special. 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.
I tried to run through the console, if the versions match JDK and JRE are the same, I attached the screen. 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. 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.
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 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 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>
Upvotes: 1
Views: 155
Reputation: 10640
Creating jar files for JavaFX programs is not the reccomended packaging practice anyway due to several issues with them. Have a look here https://stackoverflow.com/tags/javafx/info under the headline "packaging".
Upvotes: 3