简体   繁体   English

线程“JavaFX 应用程序线程”中的 JavaFx 异常 线程“主”java.lang.NoClassDefFoundError 中的异常:org/apache/log4j/Logger

[英]JavaFx Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger

The app runs perfectly fine in Eclipse and IntelliJ, and also in 'ant run'.该应用程序在 Eclipse 和 IntelliJ 以及“ant run”中运行得非常好。 Only when I run as Windows cmd to get the following errors:只有当我以 Windows cmd 运行时才会出现以下错误:

java -jar TheApp.jar java -jar TheApp.jar

    Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
        at com.th.app.ui.Login.<clinit>(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$2(LauncherImpl.java:352)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:185)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 12 more
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.NullPointerException
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:383)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        ... 5 more

This appears to be a -classpath problem, but I have spent more than one day but still getting the same error.这似乎是一个 -classpath 问题,但我已经花了一天多的时间,但仍然得到同样的错误。

I'm attaching the build.xml, which works perfectly also:我正在附加 build.xml,它也可以完美运行:

<?xml version="1.0"?>
<project name="App" default="all" basedir=".">
    <property name="src"   value="./src"/>
    <property name="build" value="./build"/>
    <property name="lib"   value="./lib"/>
    <property name="dest"   value="./dest"/>
    <property name="main-class"  value="com.th.app.ui.Login"/>

    <path id="classpath">
        <fileset dir="${lib}" includes="**/*.jar"/>
    </path>

    <target name="all" depends="clean, compile, jar, copy-file" description="Builds the whole project">
        <echo>Doing all</echo>
    </target>

    <target name="clean" description="Removes previous build">
        <delete verbose="true">
            <fileset dir="${build}"/>
        </delete>
    </target>

    <target name="compile" depends="clean" description="compile whole project">
        <echo>compile ${ant.project.name} </echo>
        <mkdir dir="${build}/classes"/>
        <copy file="./config.properties" tofile="${build}/classes/config.properties"/>
        <copy file="./src/log4j.properties" tofile="${build}/classes/log4j.properties"/>
        <copy todir="${build}/classes/com/th/app/ui">
           <fileset dir="${src}/com/th/app/ui">
                   <include name="**/*.fxml"/>
                   <include name="**/*.css"/>
           </fileset>
        </copy>

        <javac srcdir="${src}" destdir="${build}/classes" classpathref="classpath" includeantruntime="false" />
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${build}/jar"/>
        <jar destfile="${build}/jar/${ant.project.name}.jar" basedir="${build}/classes">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <property name="args" value="READWRITE"/>
    <target name="run" depends="copy-file, input-runargs">
        <java fork="true" classname="${main-class}">
            <classpath>
                <path refid="classpath"/>
                <path location="${dest}/jar/${ant.project.name}.jar"/>
            </classpath>
            <arg line="${args}"/>
        </java>
    </target>
    <target name="input-runargs" unless="args" description="prompts for command line arguments if necessary">
       <input addProperty="args" message="Type the desired command line arguments:"/>
    </target>

    <target name="copy-file">
        <copy todir="${dest}"><fileset dir="${build}"/></copy>
    </target>

</project>

This is my first JavaFx project and I'm trying hard to get it done.这是我的第一个 JavaFx 项目,我正在努力完成它。

Please help and any insight is greatly appreciated.请提供帮助,非常感谢任何见解。

You are using at least one 3rd party library (log4j), you may also be using others.您正在使用至少一个 3rd 方库 (log4j),您也可能正在使用其他库。 You need to also have those libraries on the classpath.您还需要在类路径中包含这些库。 Currently, you only have the application jar on the classpath and not the dependent libraries.目前,类路径中只有应用程序 jar,而依赖库中没有。

You could modify the manifest of your jar to provide the reference to the dependent jars.您可以修改 jar 的清单以提供对依赖 jar 的引用。

Add the following line to the manifest of your jar file.将以下行添加到 jar 文件的清单中。

Class-Path: log4j-core.jar log4j-api.jar

This can probably be done using the manifest element in the ant script (though I have not tried that).这可能可以使用 ant 脚本中的manifest元素来完成(尽管我没有尝试过)。

<manifest>
    <attribute name="Main-Class" value="${main-class}"/>
    <attribute name="Class-Path" value="log4j-core.jar log4j-api.jar"/>
</manifest>

Then ensure that the libraries are in the same directory as your jar file with the matching names you specified.然后确保这些库与您指定的匹配名称的 jar 文件位于同一目录中。

Then you can likely run the app as you were trying to do before (again I didn't try it).然后,您可能可以像以前一样运行该应用程序(同样我没有尝试过)。

java -jar TheApp.jar

Or, you could run using the -cp option and place all jars (including your jar) on the path, and run the app as:或者,您可以使用 -cp 选项运行并将所有 jar(包括您的 jar)放在路径上,然后将应用程序运行为:

java -cp TheApp.jar:log4j-core.jar:log4j-api.jar com.th.app.ui.Login

If you have other dependent libraries than just log4j, you will need to ensure they are on the classpath similarly.如果除了 log4j 之外还有其他依赖库,则需要确保它们同样位于类路径中。

There is a process called shading which unpacks all of the jar files used by your application and then repacks them into a single jar which you can execute.有一个称为着色的过程,它解压缩应用程序使用的所有 jar 文件,然后将它们重新打包到一个可以执行的 jar 中。 I do not recommend shading, but that is an alternate option which I won't detail here.我不建议使用阴影,但这是一个替代选项,我不会在此详述。

Such execution will likely only be possible on systems running obsolete versions of the JDK such as Oracle JDK 8. Recent versions of JDKs usually do not ship with the JavaFX framework, which is instead provided through a set of separate modules.这种执行可能只在运行过时版本的 JDK(如 Oracle JDK 8)的系统上才有可能。最新版本的 JDK 通常不附带 JavaFX 框架,而是通过一组单独的模块提供。 The packaging and execution of applications built using recent JavaFX versions are quite different than what you are doing for a JDK 8-based JavaFX application.使用最新 JavaFX 版本构建的应用程序的打包和执行与您为基于 JDK 8 的 JavaFX 应用程序所做的完全不同。

Advice for future applications对未来应用的建议

Use up-to-date libraries, frameworks, and development tools.使用最新的库、框架和开发工具。 Follow the official documentation for those software versions.请遵循这些软件版本的官方文档。 For instance, JDK 18+, JavaFX 18+, information at openjfx.io , Maven or Gradle instead of Ant and jlink or jpackage (usually via a Maven or Gradle plugin) for application packaging.例如,JDK 18+、JavaFX 18+、 openjfx.io上的信息、Maven 或 Gradle 而不是 Ant 以及用于应用程序打包的jlinkjpackage (通常通过 Maven 或 Gradle 插件)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“main”中的异常 java.lang.NoClassDefFoundError: org/apache/log4j/Logger 在创建可执行 JAR 后,但这是一个 JAVA 项目 - Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger after creating an executable JAR but this is a JAVA Project 线程“主”中的异常java.lang.NoClassDefFoundError:org / apache / logging / log4j / LogManager - Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager 线程“ main”中的异常java.lang.NoClassDefFoundError:org / apache / logging / log4j / message / Message - Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/logging/log4j/message/Message 线程“主”java.lang.NoClassDefFoundError 中的异常:org/apache/log4j/ProvisionNode - Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/ProvisionNode 线程“main”中的异常 java.lang.NoClassDefFoundError: org/apache/log4j/Layout - Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/log4j/Layout Scala:线程“ main”中的异常java.lang.NoClassDefFoundError:org / apache / log4j / LogManager - Scala : Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/log4j/LogManager 线程“ main”中的异常java.lang.NoClassDefFoundError:org / apache / log4j / Layout-从命令提示符处运行Runnig JAR时 - Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/log4j/Layout - While Runnig JAR from Command Prompt 线程“ JavaFX应用程序线程” java.lang.NoClassDefFoundError中的异常 - Exception in thread “JavaFX Application Thread” java.lang.NoClassDefFoundError 线程“main”中的 JavaFX 异常 java.lang.NoClassDefFoundError: javafx/application/Application - JavaFX Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application 线程“ JavaFX应用程序线程” java.lang.NoClassDefFoundError中的异常:javafx / scene / control / ButtonBar - Exception in thread “JavaFX Application Thread” java.lang.NoClassDefFoundError: javafx/scene/control/ButtonBar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM