![](/img/trans.png)
[英]Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger after creating an executable JAR but this is a JAVA Project
[英]JavaFx Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
該應用程序在 Eclipse 和 IntelliJ 以及“ant run”中運行得非常好。 只有當我以 Windows cmd 運行時才會出現以下錯誤:
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
這似乎是一個 -classpath 問題,但我已經花了一天多的時間,但仍然得到同樣的錯誤。
我正在附加 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>
這是我的第一個 JavaFx 項目,我正在努力完成它。
請提供幫助,非常感謝任何見解。
您正在使用至少一個 3rd 方庫 (log4j),您也可能正在使用其他庫。 您還需要在類路徑中包含這些庫。 目前,類路徑中只有應用程序 jar,而依賴庫中沒有。
您可以修改 jar 的清單以提供對依賴 jar 的引用。
將以下行添加到 jar 文件的清單中。
Class-Path: log4j-core.jar log4j-api.jar
這可能可以使用 ant 腳本中的manifest
元素來完成(盡管我沒有嘗試過)。
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
<attribute name="Class-Path" value="log4j-core.jar log4j-api.jar"/>
</manifest>
然后確保這些庫與您指定的匹配名稱的 jar 文件位於同一目錄中。
然后,您可能可以像以前一樣運行該應用程序(同樣我沒有嘗試過)。
java -jar TheApp.jar
或者,您可以使用 -cp 選項運行並將所有 jar(包括您的 jar)放在路徑上,然后將應用程序運行為:
java -cp TheApp.jar:log4j-core.jar:log4j-api.jar com.th.app.ui.Login
如果除了 log4j 之外還有其他依賴庫,則需要確保它們同樣位於類路徑中。
有一個稱為着色的過程,它解壓縮應用程序使用的所有 jar 文件,然后將它們重新打包到一個可以執行的 jar 中。 我不建議使用陰影,但這是一個替代選項,我不會在此詳述。
這種執行可能只在運行過時版本的 JDK(如 Oracle JDK 8)的系統上才有可能。最新版本的 JDK 通常不附帶 JavaFX 框架,而是通過一組單獨的模塊提供。 使用最新 JavaFX 版本構建的應用程序的打包和執行與您為基於 JDK 8 的 JavaFX 應用程序所做的完全不同。
對未來應用的建議
使用最新的庫、框架和開發工具。 請遵循這些軟件版本的官方文檔。 例如,JDK 18+、JavaFX 18+、 openjfx.io上的信息、Maven 或 Gradle 而不是 Ant 以及用於應用程序打包的jlink
或jpackage
(通常通過 Maven 或 Gradle 插件)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.