java - Missing JavaFX application class com.example.filesengine.MainFile не находит главный файл
У меня вылетает java проект с ошибкой:
\fe>java --module-path "D:\Рабочий стол\fe\lib\javafx-sdk-21.0.2\lib" --add-modules javafx.controls,javafx.fxml -jar FilesEngine.jar
Missing JavaFX application class com.example.filesengine.MainFile
Содержимое проекта:
Содержимое scr.json:
{
"FirstScript": "com.example.filesengine.TestScript"
}
Код MainFile:
package com.example.filesengine;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.json.simple.*;
import org.json.simple.parser.*;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class MainFile extends javafx.application.Application {
Pane layout = new Pane();
static boolean ExitUpdate = false;
@Override
public void start(Stage primaryStage) throws IOException {
primaryStage.setTitle("Hello!");
String FilesEnginePath = System.getenv("APPDATA");
FilesEnginePath = FilesEnginePath + "\\FilesEngine";
Scene scene = new Scene(layout, 300.0, 250.0);
primaryStage.setScene(scene);
primaryStage.show();
ObjectList behavior = new ObjectList();
JSONParser parserScript = new JSONParser();
try (FileReader reader = new FileReader("scr.json"))
{
//Read JSON file
java.lang.Object obj = parserScript.parse(reader);
JSONArray ListScr = (JSONArray) obj;
System.out.println(ListScr);
for (int i = 0; i == ((JSONArray) obj).size()-1; i++) {
String scr = (String) ((JSONArray) obj).get(i);
try {
Class<ObjectList> c = (Class<ObjectList>) Class.forName(scr);
Constructor<ObjectList> s = c.getDeclaredConstructor();
ObjectList w = s.newInstance();
w.startC();
} catch (java.lang.ClassNotFoundException e) {
System.out.println("Скрипт не найден!");
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
primaryStage.setOnCloseRequest(event->{ExitUpdate = true;});
behavior.update(layout);
}
public static void main(String[] args) {
launch();
}
}
pom.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>FilesEngine</artifactId>
<version>1.0-SNAPSHOT</version>
<name>FilesEngine</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.10.0</junit.version> </properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>20-ea+2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>21</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>20-ea+2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>20-ea+2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>21</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>21</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>21</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx</artifactId>
<version>20-ea+2</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>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>20-ea+2</source>
<target>20-ea+2</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.filesengine/com.example.filesengine.MainFile</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>
MainFile2:
package com.example.filesengine;
public class MainFile2 {
public static void main(String[] args) {
MainFile.main(args);
}
}
MainFile:
package com.example.filesengine;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.json.simple.*;
import org.json.simple.parser.*;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
public class MainFile extends javafx.application.Application {
Pane layout = new Pane();
static boolean ExitUpdate = false;
@Override
public void start(Stage primaryStage) throws IOException {
primaryStage.setTitle("Hello!");
String FilesEnginePath = System.getenv("APPDATA");
FilesEnginePath = FilesEnginePath + "\\FilesEngine";
Scene scene = new Scene(layout, 300.0, 250.0);
primaryStage.setScene(scene);
primaryStage.show();
ObjectList behavior = new ObjectList();
JSONParser parserScript = new JSONParser();
try (FileReader reader = new FileReader("D:\\IdeaProjects\\FilesEngine\\src\\main\\java\\com\\example\\filesengine\\scr.json"))
{
//Read JSON file
java.lang.Object obj = parserScript.parse(reader);
JSONObject jobj = (JSONObject) obj;
JSONArray scr_list = (JSONArray) jobj.get("scripts");
if (scr_list != null) {
Iterator<String> scr = scr_list.iterator();
while (scr.hasNext()) {
try {
Class<ObjectList> c = (Class<ObjectList>) Class.forName(scr.next());
Constructor<ObjectList> s = c.getDeclaredConstructor();
ObjectList WW = s.newInstance();
WW.startC();
} catch (java.lang.ClassNotFoundException e) {
System.out.println("Скрипт не найден! ClassNotFoundException");
} catch (NoSuchMethodException e) {
System.out.println("Скрипт не найден! NoSuchMethodException");
} catch (InvocationTargetException e) {
System.out.println("Скрипт не найден! InvocationTargetException");
} catch (InstantiationException e) {
System.out.println("Скрипт не найден! InstantiationException");
} catch (IllegalAccessException e) {
System.out.println("Скрипт не найден! IllegalAccessException");
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
primaryStage.setOnCloseRequest(event->{ExitUpdate = true;});
behavior.update(layout);
}
public static void main(String[] args) {
launch();
}
}
Ответы (1 шт):
Автор решения: adisteyf
→ Ссылка
Я забыл указать модуль в --add-modules.
У меня было так: java --module-path "...\javafx-sdk-21.0.2\lib" --add-modules javafx.controls,javafx.fxml -jar out\artifacts\FilesEngine_jar\FilesEngine.jar,
a надо было так: java --module-path "...\javafx-sdk-21.0.2\lib" --add-modules javafx.controls,javafx.fxml,json.simple -jar out\artifacts\FilesEngine_jar\FilesEngine.jar

