用obj= Class.forName(“mytest.user”).newInstance();

解决方案 »

  1.   

    试过了,一样提示
    java.lang.ClassNotFoundException: mytest.user
      

  2.   

    java是大小写敏感的,类名是不是大写开头阿?
      

  3.   

    那就是你的 mytest.user这个类有问题了,我一直都是这么用的没有问题的!
      

  4.   

    user只是一很简单的用来测试的类,不可能有问题的呀,直接import到工程来一点问题也没有
    package mytest;
    public class user
    {
      public user()
      {
      }  public int add(Integer a,Integer b)
      {
        return a.intValue()+b.intValue();
      }
    }
      

  5.   

    void test_load()
        {      Class obj = null;
          File file = new File("c:\\mytest.jar");
          URL url = null;      try {
            url = file.toURL();
          }
          catch (MalformedURLException ex) {
            ex.printStackTrace();
          }      URLClassLoader loader = new URLClassLoader(new URL[] {url});        try {
              obj = (Class) Class.forName("mytest.user").newInstance();
            }
            catch (ClassNotFoundException ex1) 
            {
              ex1.printStackTrace();
            }
            catch (IllegalAccessException ex1) {
            }
            catch (InstantiationException ex1) {
            }      Class partypes[] = new Class[2];
          partypes[0] = Integer.TYPE;
          partypes[1] = Integer.TYPE;
          java.lang.reflect.Method meth = null;
          String methname = "add";      try {
            meth = obj.getMethod(methname, partypes);
          }
          catch (SecurityException ex2) {
          }
          catch (NoSuchMethodException ex2) {
          }      System.out.println("get the method of the class");      Object arglist[] = new Object[2];
          arglist[0] = new Integer(37);
          arglist[1] = new Integer(47);
          Object objreturn = null;      try {
            objreturn = meth.invoke(obj, arglist);
          }
          catch (InvocationTargetException ex3) {
          }
          catch (IllegalArgumentException ex3) {
          }
          catch (IllegalAccessException ex3) {
          }      Integer retval = (Integer) objreturn;
          System.out.println(retval.intValue());    }