简体   繁体   English

Java-Java类的NoClassDefinitionFoundError

[英]Java - NoClassDefinitionFoundError for Java Class

The problem I am facing is , NoClassDefFoundError does not occur every time I run the code. 我面临的问题是,每次运行代码时都不会发生NoClassDefFoundError。 It occurs sometimes. 有时会发生。 By sometime I mean that after deploying the war in the server for 2-3 times (.ie uninstalling the earlier war and now installing and then starting the new war),on a particular call to the server(.ie for a particular web service), which uses generateHash method of class Util given below, I get this NoclassDefFoundError for the class TestJNIUtil . 到某个时候,我的意思是在服务器上进行一次特定的调用(即对于特定的Web服务)之后,在服务器中部署了2-3次war之后(即卸载先前的war然后现在安装然后开始新的war)。 ),它使用下面给出的类Util的 generateHash方法,我为TestJNIUtil类得到了这个NoclassDefFoundError

So every time this error occurs, I have to stop all the services of java and server and then restart them again.Due to this error occurring randomly, I am unable to find the reason behind this problem. 因此,每次发生此错误时,我都必须停止Java和服务器的所有服务,然后再次重新启动它们。由于此错误是随机发生的,因此无法找到此问题背后的原因。

Please help me resolve this problem. 请帮助我解决此问题。

Note: Use of Spring in the project. 注意:在项目中使用Spring。

public class Util {
    public static String generateHash(String a, String b) throws MyException{
        logger.info("In generateHash()");
        if(!StringUtil.isNullOrEmpty(a) && !StringUtil.isNullOrEmpty(b)){
            String hash = TestJNIUtil.getHashCode(a, b);
            logger.info("Inputs used a and  b  : " + a + " , " + b);
            logger.info("HashCode Generated : " + hash);
            logger.info("Out generateHash()");
            return hash;
        }
        logger.info("Out generateHash()");
        return null;
    }
}

public class TestJNIUtil{

    private static MyLogger logger = MyLoggingImpl.getLogger(TestJNI.class);

    static {
        logger.info("In static block to load DLL.");
        String dllPath = System.getenv(MyConstants.JNI_LIB);
        if(!StringUtil.isNullOrEmpty(dllPath)){
            logger.info("Loading MyJni.dll & libeay32.dll from Classpath.");
            libPath = System.getenv("JNI_LIB");
            logger.info("Library Path Used for Jni: "  + libPath);
            System.load(libPath + "\\MyJni.dll");
            logger.info("Loaded MyJni.dll Successfully.");
            System.load(libPath + "\\libeay32.dll");
            logger.info("Loaded libeay32.dll Successfully.");

        }else{
            logger.info("add JNI_LIB environment variable to load DLL.");
        }
        logger.info("Out static block to load DLL.");
    }

    public static String getHashCode(String a, String b)
            throws MyException {
        logger.info("In getHashCode().");
        String hashCode = null;
        try {
            if (a and b are not null) {
                // Call native code for hash code generation.
                hashCode = MyJni.generateHash(a, b);
                logger.info("Hash Code Generated : " + hashCode);
            } else {
                //throw MyException
            }
        } catch (MyException e) {
            //log and then throw MyException
        }
        if (StringUtil.isNullOrEmpty(hashCode)) {
            //log and then throw MyException
        }
        logger.info("Out getHashCode().");
        return hashCode;
    }
}

public class MyJNI {
    public static native String generateHash(String a, String b);
}

I have seen this kind of behavior with static initializers and war deployments to GlassFish server. 我已经看到了这种与静态初始值设定项和war部署到GlassFish服务器的行为。 Something to do with glassfish' s class loader. 与glassfish的类加载器有关。

I would remove the loading of the dlls from the static initializer and maybe put it in the context initializer of the Web app. 我将从静态初始化程序中删除dll的加载,也许将其放在Web应用程序的上下文初始化程序中。 Or even better I would add more intelligence to getHashCode method to load the dlls in case of them being not available. 甚至更好的是,我将向getHashCode方法添加更多智能,以在无法使用dll的情况下加载dll。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM