当我运行Java应用程序时,我得到了一个NoClassDefFoundError。造成这种情况的典型原因是什么?
当前回答
我有这个错误,但无法根据这个线程找出解决方案,但我自己解决了。
对于我的问题,我正在编译以下代码:
package valentines;
import java.math.BigInteger;
import java.util.ArrayList;
public class StudentSolver {
public static ArrayList<Boolean> solve(ArrayList<ArrayList<BigInteger>> problems) {
//DOING WORK HERE
}
public static void main(String[] args){
//TESTING SOLVE FUNCTION
}
}
然后,我在/ProjectName/valentines这样的文件夹结构中编译这段代码 编译它工作得很好,但试图执行:java StudentSolver
我得到了NoClassDefError。
为了解决这个问题,我简单地删除了:包装情人节礼物;
我不是很精通java包等,但这是我如何修复我的错误,非常抱歉,如果这已经由其他人回答,但我无法解释它到我的问题。
其他回答
Java中的NoClassDefFoundError
定义:
Java虚拟机无法在运行时找到在编译时可用的特定类。 如果一个类在编译时存在,但在运行时在java类路径中不可用。
例子:
The class is not in Classpath, there is no sure shot way of knowing it but many times you can just have a look to print System.getproperty("java.classpath") and it will print the classpath from there you can at least get an idea of your actual runtime classpath. A simple example of NoClassDefFoundError is class belongs to a missing JAR file or JAR was not added into classpath or sometimes jar's name has been changed by someone like in my case one of my colleagues has changed tibco.jar into tibco_v3.jar and the program is failing with java.lang.NoClassDefFoundError and I were wondering what's wrong. Just try to run with explicitly -classpath option with the classpath you think will work and if it's working then it's a sure short sign that someone is overriding java classpath. Permission issue on JAR file can also cause NoClassDefFoundError in Java. Typo on XML Configuration can also cause NoClassDefFoundError in Java. when your compiled class which is defined in a package, doesn’t present in the same package while loading like in the case of JApplet it will throw NoClassDefFoundError in Java.
可能的解决方式:
The class is not available in Java Classpath. If you are working in J2EE environment than the visibility of Class among multiple Classloader can also cause java.lang.NoClassDefFoundError, see examples and scenario section for detailed discussion. Check for java.lang.ExceptionInInitializerError in your log file. NoClassDefFoundError due to the failure of static initialization is quite common. Because NoClassDefFoundError is a subclass of java.lang.LinkageError it can also come if one of it dependency like native library may not available. Any start-up script is overriding Classpath environment variable. You might be running your program using jar command and class was not defined in manifest file's ClassPath attribute.
资源:
3种方法解决NoClassDefFoundError
noclassdeffounderror问题模式
我发现,当使用运行时发现的不兼容的类版本编译代码时,有时会得到NoClassDefFound错误。我记得的具体实例是apache axis库。在我的运行时类路径上实际上有2个版本,它正在挑选过时和不兼容的版本,而不是正确的版本,导致NoClassDefFound错误。这是在一个命令行应用程序中,我正在使用类似的命令。
set classpath=%classpath%;axis.jar
我能够让它拿起正确的版本使用:
set classpath=axis.jar;%classpath%;
每个人都在这里谈论一些Java配置的东西,JVM的问题等,在我的情况下,错误与这些主题完全无关,有一个非常琐碎和容易解决的原因:我在我的控制器(Spring Boot应用程序)的端点有一个错误的注释。
如果有人因为java.lang.NoClassDefFoundError: org/apache/log4j/Logger错误来到这里,在我的例子中,它是因为我使用了log4j 2(但我没有添加它附带的所有文件),而一些依赖库使用了log4j 1。解决方案是添加Log4j 1。X桥:log4j-1.2-api-<version>.jar,随log4j 2而来。更多信息在log4j 2迁移中。
此错误可能是由未检查的Java版本要求引起的。
在我的案例中,当我构建一个备受瞩目的开源项目时,通过使用SDKMAN从Java 9切换到Java 8,我能够解决这个错误。
sdk list java
sdk install java 8u152-zulu
sdk use java 8u152-zulu
然后按照下面的描述进行干净安装。
在使用Maven作为构建工具时,在禁用测试的情况下进行干净的“安装”构建有时是有帮助的,而且通常是令人满意的。
mvn clean install -DskipTests
现在已经构建并安装了所有内容,您可以继续运行测试。
mvn test
推荐文章
- 如何在java中格式化持续时间?(如格式H:MM:SS)
- urlencoder .encode(字符串)已弃用,我应该使用什么代替?
- javax.transaction.Transactional vs . org.springframework.transaction.annotation.Transactional
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?