猿问

有没有办法调试为什么我的 JavaFX 项目的可执行 JAR 在我的计算机上运行而不在其他计

我导出了一个 JavaFX2 项目的 Jar。它工作正常,但是当我在另一台机器上运行它时,会有一个 classDefNotFoundExeption: javafx.application.Application


任何提示如何解决这个问题?


这是我的清单:


Class-Path: .

Main-Class: proj.view.Launcher


我还编写了启动 Swing GUI 的 Launcher,以防找不到 JavaFX。


这是我的启动器类



public class Launcher {


    public static void main(String[] args) {


        try {


            Class c = javafx.application.Application.class;

            proj.main.App.main(args);



        }catch (NoClassDefFoundError e) {

            String[] t = {"Swing Backup","Application start Error"};

            MainFrame.remote(t);



        }


    }


}


桃花长相依
浏览 121回答 4
4回答

MMTTMM

其他计算机在不包含 JavaFX 的 Java 安装上运行。它可以在您的机器上运行,因为您确实安装了 JavaFX。要测试是否javafx.application.Application可用,您需要使用反射,即boolean hasJavaFX;try {    Class.forName("javafx.application.Application");    hasJavaFX = true;} catch (ClassNotFoundException e) {    hasJavaFX = false;}if (hasJavaFX) {    MainFrame.remote(new String[] {"Swing Backup","Application start Error"});} else {    proj.main.App.main(args);}

猛跑小猪

我想通了:它是指向 JDK10 bin 的用户路径变量。我们改变了路径。现在它适用于“java -jar programm.jar”但不适用于“java programm.jar”但不是通过常规单击文件。但是我们写了一个批处理文件,用“java -jar”启动应用程序,它工作正常。有没有人解释这种行为?

jeck猫

当您在 jdk 高于 11 的机器上运行时,您可能会得到classDefNotFoundExeption: javafx.application.Application 。oracle 已经从 JDK 11 中删除了 javaFX,因此需要提供对 javafx-controls 模块的依赖。`<dependency>&nbsp; &nbsp; <groupId>org.openjfx</groupId>&nbsp; &nbsp; <artifactId>javafx-controls</artifactId>&nbsp; &nbsp; <version>12-ea+9</version></dependency>`将此添加到您的依赖项中。https://openjfx.io/

眼眸繁星

对我来说,如果相同的代码在那些机器上通过 IDE 运行,那么它看起来像是可运行的 jar 问题。您可以尝试使用 maven assembly 插件来打包您的 jar。Apache Maven Assembly Plugin 允许用户将项目输出及其依赖项、模块、站点文档和其他文件聚合到一个可运行的包中。<plugin>&nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; <artifactId>maven-assembly-plugin</artifactId>&nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>package</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>single</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <archive>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <manifest>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; package.your_main_class&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </manifest>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </archive>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <descriptorRefs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <descriptorRef>jar-with-dependencies</descriptorRef>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </descriptorRefs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; </executions></plugin>
随时随地看视频慕课网APP

相关分类

Java
我要回答