Java 本来就是动态加载class的!
难道你是想:
class.forName("you class name");

解决方案 »

  1.   

    class不在classpath中,但是知道jar的路径怎么处理
      

  2.   

    不太明白你想干什么?
    编译的时候
    javac myJava.java -classpath "jar的路径;%classpath%"
    运行
    java myJava -classpath "jar的路径;%classpath%"
    这样可以不需要事先把jar的路径加入到classpath里,只是在编译和运行需要用到该jar的文件的时候才加入
      

  3.   

    不,我是在程序运行时才知道jar的的路径,也就是在运行是来load class
      

  4.   

    看你的意思好像要动态指定classpath?有难度。classpath下的东西都是在初期化的时候就已经load好了的吧。你要想load一个不在classpath下面的jar恐怕不行,即使动态copy到classpath下面,可能也不行吧。
      

  5.   

    真没有办法吗?
    可能用ClassLoader能实现,但不知具体怎么办
      

  6.   

    import java.io.*;
    import java.util.*;
    import java.util.jar.*;
    import java.lang.Exception.*;
    import java.util.zip.*;
    public class myClassLoader extends ClassLoader {
    public synchronized Class loadClass(String className,String jarpath) throws ClassNotFoundException{
    Class result = null;
    byte[] classData = null;
    try{
    classData = getByteArrayFromJarFile(className,jarpath);
    result = defineClass(className, classData, 0, classData.length);
    }catch(Exception ex){
    ex.printStackTrace();
    }
    return result;
    }
    private byte[] getByteArrayFromJarFile(String className,String jarpath){
            int len;
    byte buffer[] = new byte[1024];
    JarFile jarFile = null;
    try{
    jarFile = new JarFile(jarpath);        
    }catch(Exception ex){
    }
    className = className.replace('.','/');
            try{
                ZipEntry zipEntry = jarFile.getEntry(className+".class");
    if(zipEntry!=null){
    try{
    InputStream inputStream = jarFile.getInputStream(zipEntry);
    int arrayLength = inputStream.available();
    byte[] bytes = new byte[arrayLength]; int pos = 0;
    while (true) {
    int n = inputStream.read(bytes, pos,arrayLength - pos);
    if (n <= 0)
    break;
    pos += n;
    }
    inputStream.close();
    return bytes;
    }catch(IOException exp){
        exp.printStackTrace();
    }
    }
            }catch(Exception ex){
                //ex.printStackTrace();
                return null;
            }
    return null;
        }
    }