【问题标题】:java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils [duplicate]java.lang.NoClassDefFoundError:org/apache/commons/io/FileUtils [重复]
【发布时间】:2015-01-08 02:12:22
【问题描述】:

我正在使用 IntelliJ,并且正在尝试使用 FileUtils.copyFile() 方法。当我使用该方法时,出现此错误:

    Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apach
e/commons/io/FileUtils

这里是使用复制文件方法的代码:

import org.apache.commons.io.FileUtils;
...
    try {
         File destJAR = new File("c:/X-Dock/MP3Player.jar");
         File playerJAR = new File(MP3Player); //"MP3Player" is a string that is defined earlier.
         FileUtils.copyFile(playerJAR, destJAR);
    }catch (IOException e){
         e.printStackTrace();
    }

我通过转到

将 Apache Commons JAR“commons-io-2.4”导入 IntelliJ 项目
File -> Project Structure -> Libraries -> Add -> Java -> "commons-io-2.4.jar"

任何帮助将不胜感激。

【问题讨论】:

  • 该错误意味着 commons-io-2.4.jar 在运行时不在您的类路径中。你是如何运行你的程序的?
  • @ruakh 我构建了一个可执行的 jar 文件并运行它。或者我只是从 IntelliJ 运行它。我像许多其他外部库一样导入了 commons-io-2.4.jar
  • 运行可执行 jar 时,如何指定类路径?
  • @ruakh 我不完全确定你的意思。你能解释一下吗
  • 构建过程(可能)没有将 commons-io-2.4.jar 嵌入到您自己的 jar 文件中。相反,它希望commons-io-2.4.jar 在运行时可用;所以你自己的 jar 文件依赖它,但不提供它。在运行时使其可用的方法是将其包含在类路径中。例如,如果您正在运行 java,您将使用 -cp 参数来指定类路径。

标签: java apache noclassdeffounderror


【解决方案1】:

请按照以下步骤解决您的问题。

或者你可以编辑配置文件

build.gradle

dependencies {
    compile files('libs/commons-io-2.4.jar')
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多