从终端运行JavaFX时出错-无法找到或加载主类 [英] Error while running JavaFX from terminal - Could not find or load main class

查看:443
本文介绍了从终端运行JavaFX时出错-无法找到或加载主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些巧妙的建议来解决此问题-好吧,使用简单的JavaFX应用程序时会出现某种问题.但是,在全部设置好后(例如模块路径),存在一些问题,但是现在当我无法从终端运行.jar文件时该怎么办?好的,开始讨论使用Javafx不可能实现的任务:

Need some clever advice to solve this - well, some kind of problem while using simple JavaFX app. However, there was some problems while all is set-up (module path, for example), but what now when I just cannot run .jar file from terminal? Ok, start talk about mission-impossible-with-javafx:

我在Linux Mint 20上安装了Apache NetBeans12.已经安装了JDK,因此"java -version"返回"openjdk版本"11.0.7"".2020-04-14'...启动项目时完全没有问题-JavaFX应用程序可以正常工作.

I got Apache NetBeans 12 installed on Linux Mint 20. JDK is already installed, so 'java -version' returns 'openjdk version "11.0.7" 2020-04-14'... When start project there is no problem at all - JavaFX app works.

我需要该应用程序才能在Windows 8.1上运行,但是在此之前,我尝试使用 java -jar MyFile.jar 在我的Linux终端上运行此.jar文件.问题开始于此:在MyFile.jar 中说没有主清单属性.然后,我决定直接在jar文件(使用Archive Manager打开)中设置主类:MyFile.jar->META-INF->MANIFEST.MF并在最后一行添加: Main-Class:mypackage.MyClass

I need this app to work on Windows 8.1, but before that I tried to run this .jar file on my linux terminal using java -jar MyFile.jar. Here the problem starts: it said no main manifest attribute, in MyFile.jar. Then I decide to set the main class directly in jar file (opened using Archive Manager): MyFile.jar -> META-INF -> MANIFEST.MF and add on last line: Main-Class: mypackage.MyClass

现在我有:错误:找不到或加载主类mypackage.MyClass原因:java.lang.NoClassDefFoundError:javafx/application/Application

好吧,如何以及为什么?

Ok, how and why?

仅需说一句,在NetBeans中,我还将主类设置为:项目->属性->运行->主类:mypackage.MyClass 在VM选项中我添加了:-模块路径"/locationToJavaFX/javafx-sdk-11.0.2/lib";--add-modules javafx.controls,javafx.fxml

Just to said that in NetBeans I also set main class in: Project -> Properties -> Run -> Main class: mypackage.MyClass and in VM options I added: --module-path "/locationToJavaFX/javafx-sdk-11.0.2/lib" --add-modules javafx.controls,javafx.fxml

在pom文件中,还设置了主类(由IDE生成):

In pom file, main class is also set (generated by IDE):

//  other lines
<groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.4</version>
                <configuration>
                    <mainClass>mypackage.MyClass</mainClass>
                </configuration>
//  other lines

和module-info看起来像:

and module-info looks like:

module mypackage {
    requires javafx.controls;
    requires javafx.fxml;
    requires org.jsoup;
    
    opens mypackage to javafx.fxml;
    exports mypackage;      // some people advice without this line, I tried but same problem
}

只想弄清楚-我尝试了stackoverflow上可用的所有内容(我不计算).我尝试在昨天和今天(将近2天)内解决此问题,但我做不到.

Just want to be clear - I tried probably everything (I don't count) that is available here on stackoverflow. I try to fix this yesterday and today (almost 2 whole days) and I can't.

只是想一想:Java编程并不像配置那样困难.欢呼,感谢您的帮助!

Just a thought: Java programming is not so difficult as configuration is. Cheers and thanks for helping!

只是说我尝试了何塞建议:首先创建启动器类,逐步执行胖子罐",然后运行他添加到帖子中的半胖子罐"命令.有趣的是,这返回了我:在启动层java.lang.module.FindException初始化期间发生错误:未找到模块javafx-fxml .如果我如上所述在Aready中定义了javafx-fxml,为什么不找到它呢?另外,为什么Netbeans不在清单文件(在jar中)中为我提供主类?当我在清单中添加主类时,我得到以下信息:

Just to said that I tried what José suggested : first creating launcher class, follow 'fat jar' step-by-step then run 'semi-fat jar' command which he added to the post. Interestingly enough, this return me: Error occurred during initialization of boot layer java.lang.module.FindException: Module javafx-fxml not found. Why not found javafx-fxml if I aready defined it as said above in question? Also, why Netbeans doesn't provide me the main class in manifest (in jar) file? When I add main class in manifest, I get this:

Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at Main.main(Main.java:5) Caused by: java.lang.ClassNotFoundException: javafx.application.Application
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 10 more

好,怎么找不到应用程序?:)

Ok, how Application does not found? :)

是的,何塞先生拥有权利.实际上,该命令在他的半胖罐子"中有效,但不适用于Maven(我在同一个Ant项目上使用了它,并且有效).Maven无法识别JSoup类(即使我将其添加到依赖项中),java也返回:

EDIT 2: Yeap, mister José has a right. This command in his 'semi-fat jar' post actually works BUT not for Maven (I use it on same Ant project and it works). Maven somehow cannot recognize JSoup class (even I add it in dependency) and java returns:

Exception in Application start method Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834) Caused by: java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at p.eis.GetData1.startIt(GetData1.java:34)
at p.eis.GetData1.<init>(GetData1.java:28)
at p.eis.SampleController.initialize(SampleController.java:37)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at p.eis.Start.loadFXML(Start.java:32)
at p.eis.Start.start(Start.java:19)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 17 more

希望我会在不久的将来找到Maven项目的解决方案.

Hope I'll find solution for Maven projects in the near future.

推荐答案

这是因为我无法添加更多链接,所以我再次写了这个WinRar创建Jar文件

this is because i can't add more links that's why i again wrote this WinRar to create Jar File

从上一个答案继续,图像为 https://i.stack.imgur.com/fY1tw.png

Continue From Previous Answer and image that is https://i.stack.imgur.com/fY1tw.png

这就是我从项目资源文件夹中复制的内容

只需将资源目录的所有文件粘贴到(在我的情况下)C:\ Users \ADMIN \ IdeaProjects \ SmartSeizeing \ out \ artifacts \ SmartSeizeing_jar \ smartSeizeing

WinRar使Jar跟随波纹管步骤(IMAGE)

步骤1

步骤2选择zip选项,然后将文件.zip的扩展名更改为.jar

步骤3

这就是我要向您解释的内容,这是我的输出结果

我希望您了解我如何创建jar文件

这篇关于从终端运行JavaFX时出错-无法找到或加载主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆