This is the second post by Mattis, diving deep into JVM specifics. NoClassDefFoundErrors are a drag. The classloader mechanism in the Java specification is very powerful, but it also gives you plenty of ways to mess things up. In which jar did you put that class file, and why isn't your classloader looking in that jar? In rare cases, you might even have an application that works using Sun Java, but throws a NoClassDefFoundError with JRockit. Surely, this must be a JRockit bug? Not necessarily. There is a slight difference in how the two JVMs work that can explain this behaviour, especially if you modify your classloaders during runtime. Let's take an example: In a separate folder "foo", create a file Foo.java:

public class Foo {
public Foo () {
System.out.println("Foo created");
}
}

Now, in your root folder for this experiment, create the file ClasspathTest.java:

import java.io.File;
import java.net.URLClassLoader;
import java.net.URL;
import java.lang.reflect.Method; public class ClasspathTest {
private static final Class[] parameters = new Class[]{URL.class}; // Adds a URL to the classpath (by some dubious means)
// method.setAccessible(true) is not the trademark of good code
public static void addURL(URL u) throws Exception {
Method method = URLClassLoader.class.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke((URLClassLoader) ClassLoader.getSystemClassLoader(), new Object[]{u});
} public static void main(String[] arg) throws Exception{
// Add foo to the classpath, then create a Foo object
addURL(new File("foo").toURL());
Foo a = new Foo();
}
}

This class has a method "addURL" that basically adds a URL to the classpath of the system classloader. The main method uses this method to first add the folder "foo" to the classpath and then creates a Foo object. When you compile this method, add "foo" to the classpath:

> javac -classpath .;foo Test.java

But when you run the program, don't add foo, simply run

> java Test 

Using Sun Java, this will work fine. In the first line of main, we add the foo-folder to the classpath. When we create our first Foo-object, we find the Foo class in the foo folder. Using JRockit however, you get:

Exception in thread "Main Thread" java.lang.NoClassDefFoundError: Foo
at ClasspathTest.main(ClasspathTest.java:20)

To understand this behaviour, you have to first understand how Sun and JRockit runs code. Sun Java is an interpreting JVM. This means that the first time you run a method, the JVM will interpret every line step by step. Therefore, Sun will first interpret and run the first line of main, adding "foo" to the classpath, and then the second line, creating the Foo object. JRockit however uses another strategy. The first time a method is run, the entire method is compiled into machine code. To do this, all classes used in the method needs to be resolved first. Therefore, JRockit tries to find the Foo class BEFORE the "foo" folder is added to the classpath, resulting in the NoClassDefFoundError (still thrown just before trying to use the class). So, who is right? Actually, according to the Java spec, both are. Resolving the classes can be done either right before the class is used or as early as during method invocation. For most developers, this is just trivia, but from time to time we see problems with this from customers. The solution? Don't modify your classloaders in the same method as you need the change to load a class. In the example, the following change works fine in both Sun and JRockit:

    public static void main(String[] arg) throws Exception{
// Add foo to the classpath, then create a Foo object in another method
addURL(new File("foo").toURL());
useFoo();
} public static void useFoo() {
Foo a = new Foo();
}

Here, using JRockit, the class is not resolved until the method useFoo is compiled, which will be AFTER "foo" is added to the classpath. /Mattis PS: Adding URLs to the system classloader during runtime might not be a good idea. But when using your own defined classloaders, modifying these during runtime could very well be according to design.

Why won't JRockit find my classes的更多相关文章

  1. 代码的坏味道(9)——异曲同工的类(Alternative Classes with Different Interfaces)

    坏味道--异曲同工的类(Alternative Classes with Different Interfaces) 特征 两个类中有着不同的函数,却在做着同一件事. 问题原因 这种情况往往是因为:创 ...

  2. eclipse中的classes文件夹同步问题

    问题: 在同步项目时,由于误操作将classes文件夹加入到了同步版本中,这样会导致每次更新程序编译后,会有很多class文件显示在同步清单中. 解决方案: 将classes文件不设置为同步. 1. ...

  3. Introduction of OpenCascade Foundation Classes

    Introduction of OpenCascade Foundation Classes Open CASCADE基础类简介 eryar@163.com 一.简介 1. 基础类概述 Foundat ...

  4. 6.Configure Domain Classes(配置领域类)【EF Code-First 系列】

    在前面的部分中,我们学习了Code-First默认约定,Code-First使用默认的约定,根据你的领域类,然后生成概念模型. Code-First模式,发起了一种编程模式:约定大于配置.这也就是说, ...

  5. app:clean classes Exception

    Error:Execution failed for task ':app:clean'.> Unable to delete directory: C:\Users\LiuZhen\Deskt ...

  6. Android framework编译出来的jar包classes.jar的位置

    在源码环境下编译 Android framework编译出来的jar包classes.jar的位置  out/target/common/obj/JAVA_LIBRARIES/framework_in ...

  7. yii 核心类classes.php详解(持续更新中...)

    classes.php在yii运行的时候将被自动加载,位于yii2文件夹底下. <?php /** * Yii core class map. * * This file is automati ...

  8. Top 15 Java Utility Classes

    In Java, a utility class is a class that defines a set of methods that perform common functions. Thi ...

  9. Eclipse下无法自动编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法(转载)

    文章来源:http://www.cnblogs.com/xfiver/archive/2010/07/07/1772764.html 1.  IOException parsing XML docum ...

随机推荐

  1. Deep Learning 学习笔记(9):主成分分析( PCA )与 白化( whitening )

    废话: 这博客有三个月没更新了. 三个月!!!尼玛我真是够懒了!! 这三个月我复习什么去了呢? 托福………… 也不是说我复习紧张到完全没时间更新, 事实上我甚至有时间打LOL. 只是说,我一次就只能( ...

  2. 深入浅出 Java Concurrency (10): 锁机制 part 5 闭锁 (CountDownLatch)

    此小节介绍几个与锁有关的有用工具. 闭锁(Latch) 闭锁(Latch):一种同步方法,可以延迟线程的进度直到线程到达某个终点状态.通俗的讲就是,一个闭锁相当于一扇大门,在大门打开之前所有线程都被阻 ...

  3. 2018-2019-2 《网络对抗技术》Exp7 网络欺诈防范 Week10 20165233

    Exp7 网络欺诈防范 目录 一.基础问题 二.实验步骤 实验点一:简单应用SET工具建立冒名网站 实验点二:ettercap DNS spoof 实验点三:结合应用两种技术,用DNS spoof引导 ...

  4. centos7.3安装python3.6.5

    最近在玩django,想部署个网站试试,结果发现线上默认的centos用得居然是python2.7.5,那么先升级下吧,到python3.6.5 yum安装时python2.7.5 那么编译安装吧 那 ...

  5. Bower 使用

    Bower:客户端库管理工具 来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 目录 概述 常用操作 项目初始化 库的安装 库的搜索和查看 库的更新和卸载 列出所有库 ...

  6. Django中的DRF框架视图集使用

    1. 两个基类 1)APIView Django REST_framework 中所有视图的基类是APIView   父类是View 支持定义的属性: authentication_classes 列 ...

  7. 使用tcmalloc编译出现undefined reference to `sem_init'

    tcmalloc是Google开源的一个内存管理库, 作为glibc malloc的替代品,效率大概是gclibc malloc的几倍.想在工程中用上tcmalloc非常的简单,我们采用了静态编译的方 ...

  8. Linux实战教学笔记27:Nginx详细讲解

    前言:nginx的特点 本节主要对Nginx Web服务软件进行介绍,涉及Nginx的基础,特性,配置部署,优化,以及企业中的日常运维管理和应用.作为HTTP服务软件的后起之秀,Nginx与它的老大哥 ...

  9. java.lang.NoSuchMethodError: org.springframework.dao.IncorrectResultSizeDataAccessException

    spring data jpa  运用,在dao类中写自己新增的方法,使用@query写hql语句,出现以下异常: Caused by: java.lang.NoSuchMethodError: or ...

  10. 微信小程序开发教程,大多数人都搞错的八个问题

    小程序目前被炒得沸沸扬扬,无数媒体和企业借机获取阅读流量. 这再次证明一点,微信想让什么火,真的就能让什么火.这种能力真是全中国再也没有人有了,政府也没有. 但四处传的消息很多是失真的,废话不说,先列 ...