跳至主要内容

博文

目前显示的是标签为“classload”的博文

Class Not Found vs No Class Def Found

Class Not Found vs No Class Def Found When we are working on JavaEE, we will always has multiple issues related to classes and ClassLoader . We may struggle to find some strange memory leaks caused by ClassLoader ; we may fail to run some applications because it fail to find some class; we may encounter cases when multiple versions of library conflict. In order to solve all similar problems one for all, today we focus the understanding of two exception (to be precise, one exception and one error): java.lang.ClassNotFoundException java.lang.NoClassDefFoundError ClassLoader Before we get to solve the problem, we have to skim some basic knowledge of class loading. In the world of Java, almost all things are abstracted into a class. Class is also a class, which organized by classpath and package structure. In order to load classes when suitable, JVM use the abstraction of ClassLoader to locate or generate data that constitutes a definition for the class. Th...

Design a Logging System?

Design a Logging System? Long long ago, when there is not any mature logging system, Tom is facing the problem of logging. The native logging system form Java SDK has some issues of class loading, in which it may cause memory leaks 1 . Requirement He discussed this problem with leader and they decided to write their own logging system with the following requirements: The logging system can be used by different package, class at the same time and we can easily differentiate them; The logging system can output to different location, for example, console, file or even network; The logging system can output formatted log content: plain text, JSON, XML etc; The logging system can have different levels to control the behavior in different environments: in development env, we can log more, but in production env, we maybe log only exception; Design Tom now begin to analyze the requirements of logging system using OO related patterns and try to make some basic...

ClassLoading In JavaEE

Today, we are going to talk about class loader in the JavaEE environment. We start from the normal ClassLoader hierarchy in JavaSE application, then analyze the differences between JavaEE. ClassLoader Hierarchy In a normal JavaSE application, we have three levels of ClassLoader: Boostrap ClassLoader Extension ClassLoader System ClassLoader Because all classes in Java are loaded by ClassLoader, we have to solve the problem: how is the first class in Java loaded? The answer is simple if we jump out of the scope of a single language. The first class in Java is loaded by a c++ ClassLoader, and because its this functionality, it is called ‘Boostrap’ ClassLoader. Besides the first class, all the classes of Java core library are all loaded by this ClassLoader in the aim of security. Extension ClassLoader, as it name indicates, is used to load installed extensions under lib/ext of jdk home. System ClassLoader, on the other hand, is used to load application specific classe...

How to Ref Files in Jar

Recently, we are working on a project which will reference some config file used in Jar. In order to reference those file, we can’t use those files absolute path (because the packaged Jar may use in any computer which will invalid the path) or relative path as we run it in a IDE (for packaged Jar has different file structure with that in IDE. So, Java provide a mechanism to reference files: getResource Get Resource via Different Ways By a simple search, we can see that there exists two main ways of doing so: this .getClass().getResource( "file" ); this .getClass().getClassLoader().getResource( "file" ); And common questions arises: “what the differences between them?” Let’s have a look at the following code snippet: // get file of foo/bar/name foo.bar.A.class.getResource( "name" ) // get file of `A's classloader cwd`/name foo.bar.A.class.getResource( "/name" ) // get file of `A's classloader cwd`/name foo.bar.A.cla...

ClassNotFound when deploy war

Today, when I run the tomcat web project in intellij, tomcat fail to start up with error: NoClassDefFoundError: xxxx ... Caused by: ClassNotFoundException: xxxx Steps to solve Check artifact First we check whether the class is in the artifact. Because intellij use artifact to build jar/war to deploy, if artifact not have this class, tomcat will certainly not find it. What should notice is that, even it compile well doesn’t mean it can run if you not include it in right classpath for ClassLoader to find. In my case, I find the dependency of a jar file is upgraded and I didn’t update maven. I update maven repository using the following command: mvn clean package -U But after update, the library in artifact is still the old version( in essence, the intellij use xml files under .idea/artifacts/ to config the artifact, and the setting is still the old version – seems a bug of intellij ). So I have to update the intellij artifact setting to make it right manually. ...

Annotations in JBoss Deployment

在我们书写EJB代码时,为了简化配置,我们使用了注解而不是xml配置文件。那么,JBoss(或者说Wildfly)是如何利用这些注解,在合适的时候调用了我们的代码呢?换句话说,jboss从启动到输出像这样的jndi设置信息的部署过程究竟发生了什么?今天我们就通过分析其源代码简单探讨一下这个问题。 从annotations讲起 在开发过程中, 我们也时常会在应用代码中会看到这些看上去比较奇怪的@开头的东西,例如@Override, @Deprecated。他们的作用到底是什么?又是如何实现的呢?我们稍稍介绍以一跟我们今天内容相关的背景。 注解,根据其寿命长短,可以分为三类: Source:只存在于源代码中,在编译后便被丢弃。所以说他的作用范围便是编译时。 Class:在class文件中,但不在VM中。貌似没有什么卵用 Runtime:一直存活到VM中,即可以通过反射获得注释信息(用大白话说就是,当你程序跑起来之后,还可以获取注解的内容)。 所以,联想到我们在使用EJB的过程:先写好源代码,再编译,再打包,再交给jboss部署,jboss再调用我们的代码。我们可以明白,诸如@Stateless, @EJB这样的注解都是第三类注解。 JBoss部署 在上面讲的使用EJB的过程中,jboss的部署其实还是一个黑盒,所以当我们把我们的jar包部署到JBoss时究竟发生了什么? 让我们通过源代码(org/jboss/as/server/deployment/Phase.java)把部署过程理一理: /** * This phase creates the initial root structure. Depending on the * service for this phase will ensure that the * deployment unit's initial root structure is available and accessible. * Upon entry, this phase performs the following actions: * <ul> * <li>The primary deployment root is mounted...

Singleton and Synchronization

Why synchronized in singleton I will describe what I encounter to convince you it is really needed to do so. I have the Class A in the Thread-use : Singleton s1 = Singleton.getInstance(); synchronized(s1){ while (!B.ready){ s1.wait(); } // read from singleton } Then Class B in Thread-readData : // notice the volatile static volatile boolean ready = false; Singleton s2 = Singleton.getInstance(); synchronized(s2){ // read data than write to singleton ready = true; s2.notify(); } Then Singleton : public static Singleton getInstance() { if(instance == null) { instance = new Singleton(); } return instance; } This getInstance() is not synchronized now. If the first thread enter the if clause first, then the second thread get the cpu also enter it ( if clause ) when the instance are still null , so they create two Singleton and return separately . As a result, the s1 and s2 point to the different objects which cause...