0 0

关于javassist.NotFoundException20

AOP中需要通过反射获取方法参数名称,所以用到javassist,jar包测试ok
可放到war包,部署到tomcat后便报异常:javassist.NotFoundException: xxx.xxx.xxxServiceImpl
war包中配的AOP拦截jar包中的Service方法,jar已通过maven引入到war中。

javassist代码:
ClassPool pool = ClassPool.getDefault();
CtClass cls = pool.get(clazz.getName());  // 此处报异常

不知哪位有这方面经验往指教,谢谢。

问题补充:
chenxiang105 写道
引用

war 中都是些什么?

jsp clases lib库文件?



:(  war也是一种打包方式 可以丢在tomcat weapp下直接运行项目

jar可以程序没有问题, jar既然引入了就应该可以使用.
再报错的地方打log 看那个getname() 是个什么东西

在启动的时候检查xxx/xxx/xxxServiceImpl.class文件在启动后webapp下面项目中是否可以找到
映射到配置文件是否可以找到 是否正确



getname() 获得的是全称类名,debug过了,是没有问题的。
xxxServiceImpl.class 存在,不通过javassist反射是可以调用的。

这个问题貌似是类因为在不同的项目中,所以javassist找不到类文件

有个insertClassPath方法,但不知道怎样正确使用。。。测试了半天依然报错。。。杯具。。。




问题补充:看了官方对这个问题的说明,还没有试,希望对以后遇到该问题的人有所帮助,如下:

The default ClassPool returned by a static method ClassPool.getDefault() searches the same path that the underlying JVM (Java virtual machine) has. If a program is running on a web application server such as JBoss and Tomcat, the ClassPool object may not be able to find user classes since such a web application server uses multiple class loaders as well as the system class loader. In that case, an additional class path must be registered to the ClassPool. Suppose that pool refers to a ClassPool object: 

pool.insertClassPath(new ClassClassPath(this.getClass()));

This statement registers the class path that was used for loading the class of the object that this refers to. You can use any Class object as an argument instead of this.getClass(). The class path used for loading the class represented by that Class object is registered. 

You can register a directory name as the class search path. For example, the following code adds a directory /usr/local/javalib to the search path: 

ClassPool pool = ClassPool.getDefault();
pool.insertClassPath("/usr/local/javalib");

The search path that the users can add is not only a directory but also a URL: 

ClassPool pool = ClassPool.getDefault();
ClassPath cp = new URLClassPath("www.javassist.org", 80, "/java/", "org.javassist.");
pool.insertClassPath(cp);

This program adds "http://www.javassist.org:80/java/" to the class search path. This URL is used only for searching classes belonging to a package org.javassist. For example, to load a class org.javassist.test.Main, its class file will be obtained from: 

http://www.javassist.org:80/java/org/javassist/test/Main.class

Furthermore, you can directly give a byte array to a ClassPool object and construct a CtClass object from that array. To do this, use ByteArrayClassPath. For example, 

ClassPool cp = ClassPool.getDefault();
byte[] b = a byte array;
String name = class name;
cp.insertClassPath(new ByteArrayClassPath(name, b));
CtClass cc = cp.get(name);

The obtained CtClass object represents a class defined by the class file specified by b. The ClassPool reads a class file from the given ByteArrayClassPath if get() is called and the class name given to get() is equal to one specified by name. 

If you do not know the fully-qualified name of the class, then you can use makeClass() in ClassPool: 

ClassPool cp = ClassPool.getDefault();
InputStream ins = an input stream for reading a class file;
CtClass cc = cp.makeClass(ins);

makeClass() returns the CtClass object constructed from the given input stream. You can use makeClass() for eagerly feeding class files to the ClassPool object. This might improve performance if the search path includes a large jar file. Since a ClassPool object reads a class file on demand, it might repeatedly search the whole jar file for every class file. makeClass() can be used for optimizing this search. The CtClass constructed by makeClass() is kept in the ClassPool object and the class file is never read again. 

The users can extend the class search path. They can define a new class implementing ClassPath interface and give an instance of that class to insertClassPath() in ClassPool. This allows a non-standard resource to be included in the search path. 
2011年3月18日 17:28

5个答案 按时间排序 按投票排序

0 0

跟类加载器有关

2014年8月26日 00:40
0 0

请问楼主该问题解决了么?

2014年2月26日 11:20
0 0

  你前面加上路径试试. 在全名前面

2011年3月18日 18:13
0 0

引用

war 中都是些什么?

jsp clases lib库文件?



:(  war也是一种打包方式 可以丢在tomcat weapp下直接运行项目

jar可以程序没有问题, jar既然引入了就应该可以使用.
再报错的地方打log 看那个getname() 是个什么东西

在启动的时候检查xxx/xxx/xxxServiceImpl.class文件在启动后webapp下面项目中是否可以找到
映射到配置文件是否可以找到 是否正确

2011年3月18日 17:55
0 0

war 中都是些什么?

jsp clases lib库文件?

2011年3月18日 17:34

相关推荐

    javassist.jar

    Java程序报错:Caused by: java.lang.NoClassDefFoundError: javassist/bytecode/ClassFile 时,可引入该包解决。可能有不同版本和大小的区别。

    javassist.jar 最新3.22.0-GA正式版

    Javassist.jar是一个可以执行字节码操作的函数库,可是尽管如此,它却是简单而便与理解的。他允许开发者对自己的程序自由的执行字节码层的操作,当然了,你并不需要对字节码有多深的了解,或者,你根本就不需要了解...

    Android代码-javassist

    Javassist version 3 Copyright (C) 1999-2018 by Shigeru Chiba, All rights reserved. Javassist (JAVA programming ASSISTant) makes Java bytecode manipulation simple. It is a class library for editing ...

    javassist.jar源码

    ` javassist.jar `通常与Java反射API一起使用,提供了一种更强大的方式来控制程序的运行时行为。 5. **性能**:Javaassist的性能比基于源代码的字节码操作工具(如ASM或BCEL)稍逊一筹,因为它的操作基于源代码级别...

    Javassistjar包

    关于java字节码的处理,目前有很多工具,如bcel,asm。不过这些都需要直接跟虚拟机指令打交道。如果你不想了解虚拟机指令,可以采用javassist。javassist是jboss的一个子项目,其主要的优点,在于简单,而且快速。...

    javassist-3.27.0-GA-API文档-中英对照版.zip

    赠送jar包:javassist-3.27.0-GA.jar; 赠送原API文档:javassist-3.27.0-GA-javadoc.jar; 赠送源代码:javassist-3.27.0-GA-sources.jar; 赠送Maven依赖信息文件:javassist-3.27.0-GA.pom; 包含翻译后的API文档...

    javassist-3.18.1-GA.jar

    hibernate依赖的包javassist-3.18.1-GA.jar命名存在,这些jar包,其中的javassist.ClassPath存在,

    javassist.7z

    Java bytecode engineering toolkit,Javassist version 3. Javassist (JAVA programming ASSISTant) makes Java bytecode manipulation simple. It is a class library for editing bytecodes in Java; it enables ...

    动态代理-jdk、cglib、javassist.zip

    本压缩包包含关于三种主要的动态代理实现方式:JDK动态代理、CGLIB以及javassist的相关资料。 首先,JDK动态代理是Java标准库提供的一种动态代理机制,它依赖于java.lang.reflect包中的Proxy和InvocationHandler...

    javassist-3.23.1-GA-API文档-中英对照版.zip

    赠送jar包:javassist-3.23.1-GA.jar; 赠送原API文档:javassist-3.23.1-GA-javadoc.jar; 赠送源代码:javassist-3.23.1-GA-sources.jar; 赠送Maven依赖信息文件:javassist-3.23.1-GA.pom; 包含翻译后的API文档...

    javassist-3.11.0.GA.jar

    stuts2项目启动tomcat报错:Error configuring application listener of class org.apache.struts2.dispatcher.ng.listener.StrutsListener,缺少的是这个javassist-3.11.0.GA.jar

    javassist-3.23.1-GA-API文档-中文版.zip

    赠送jar包:javassist-3.23.1-GA.jar; 赠送原API文档:javassist-3.23.1-GA-javadoc.jar; 赠送源代码:javassist-3.23.1-GA-sources.jar; 赠送Maven依赖信息文件:javassist-3.23.1-GA.pom; 包含翻译后的API文档...

    javassist-3.7.ga.jar下载

    `javassist-3.7.ga.jar`是Javaassist的一个版本,ga表示General Availability,意味着这是一个正式发布的稳定版本。 Javaassist的核心功能包括: 1. 字节码操作:Javaassist提供了一种高级的API,允许开发者在运行...

Global site tag (gtag.js) - Google Analytics