程序很简单,就是从使用反射,从一个存在的class里面读出来所有的methods,程序如下import java.lang.reflect.*;
import java.net.URL;
import java.net.URLClassLoader;
public class SAXStruts_config {
    public static void main(String args[]) {
        try {
URLClassLoader urlcl = new URLClassLoader(new URL[]{new URL("file://f:/ref/")});
         Class c = urlcl.loadClass("com.oreilly.struts.storefront.order.ShoppingCartActions");            Method m[] = c.getDeclaredMethods();
            for (int i = 0; i < m.length; i++)
                System.out.println(m[i].getName());
        } catch (Throwable e) {
            System.err.println(e);
        }
    }
}可是编译的时候总会报
java.lang.NoClassDefFoundError: org/apache/struts/actions/DispatchAction看起来好像是classpath的问题,刚接触反射,不太熟悉,谢谢下面是ShoppingCartActions 
package com.oreilly.struts.storefront.order;import java.io.IOException;
import java.text.Format;
import java.text.NumberFormat;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;
import com.oreilly.struts.storefront.service.IStorefrontService;
import com.oreilly.struts.storefront.catalog.view.ItemDetailView;
import com.oreilly.struts.storefront.framework.UserContainer;
import com.oreilly.struts.storefront.framework.util.IConstants;
import com.oreilly.struts.storefront.framework.ShoppingCartItem;
import com.oreilly.struts.storefront.framework.ShoppingCart;
import com.oreilly.struts.storefront.framework.StorefrontDispatchAction;
/**
 * Implements all of the functionality for the ShoppingCart.
 */
public class ShoppingCartActions extends DispatchAction {
  /**
   * This method just forwards to the success state, which should represent
   * the shoppingcart.jsp page.
   */
  public ActionForward view(ActionMapping mapping,
                            ActionForm form,
                            HttpServletRequest request,
                            HttpServletResponse response)
    throws Exception {。。

解决方案 »

  1.   

    那这种问题一般怎么解决呢?我java不是太好,谢谢
      

  2.   

    DispatchAction没有找到
    看看你下载的struts相关的包,你确定已经把他们都引用到你的工程文件中???
      

  3.   

    应该是类的路径设置错误,如果com.oreilly.struts.storefront.order.ShoppingCartActions是某个包里的类,那么可能没有对这个包添加引用。
      

  4.   

    恩,类路径问题,就是说你反射的那个类ShoppingCartActions 关联了DispatchAction 这个类,但是编译器又无法找到DispatchAction 所以就抱错了
    需要把struts的相关jar包全部引入到classpath,用JB等IDE开发的话,引入到工程的libpath就可以了
      

  5.   

    缺少包含org/apache/struts/actions/DispatchAction的包,把它加到classpath里就可以了