这是部分代码
Constructor<ExternalAttachmentProvider> constructor=ExternalAttachmentProvider.class.getConstructor(Bus.class);(出错位置)
ExternalAttachmentProvider eap =(ExternalAttachmentProvider)constructor.newInstance(bus);
ExternalAttachmentProvider 的部分源码
public class ExternalAttachmentProvider extends AbstractPolicyProvider
    implements PolicyProvider {
    
    private static final ResourceBundle BUNDLE = BundleUtils.getBundle(ExternalAttachmentProvider.class);
    
    // Use a Resource object here instead of a String so that the resource can be resolved when
    // this bean is created
  
    private Resource location;
    private Collection<PolicyAttachment> attachments;
    
    ExternalAttachmentProvider() {        
    }
    
    ExternalAttachmentProvider(Bus b) {
        super(b);
    }
    。
报错为
Exception in thread "main" java.lang.NoSuchMethodException: org.apache.cxf.ws.policy.attachment.external.ExternalAttachmentProvider.<init>(org.apache.cxf.Bus)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getConstructor(Unknown Source)
at demo.ws_policy.server.Server.<init>(Server.java:59)
at demo.ws_policy.server.Server.main(Server.java:97)
不知道这是错误?请各位指教

解决方案 »

  1.   

    Exception in thread "main" java.lang.NoSuchMethodException:没有这个方法。
    是否引用的jar不是最新的或不是合适的
      

  2.   

    ExternalAttachmentProvider 有这个构造器,只不过是默认权限,不知道是不是这个原因。
    ExternalAttachmentProvider这个类跟执行类在不同的包中。
      

  3.   

    就是这个原因,但是和是不是在一个包没有关系,参考getConstrutor(Class<?>...)的javadoc
        /**
         * Returns a <code>Constructor</code> object that reflects the specified
         * public constructor of the class represented by this <code>Class</code>
         * object. The <code>parameterTypes</code> parameter is an array of
         * <code>Class</code> objects that identify the constructor's formal
         * parameter types, in declared order. 
      

  4.   

    请使用getDeclaredConstructor,
    Constructor<X> constructor = X.class.getDeclaredConstructor(...);
    // 解决访问权限的问题
    constructor.setAccessible(true);
    constructor.newInstance(...)