当前位置: 网学 > 编程文档 > JAVA > 正文

了解 JAVA classloader

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/10/16
下载{$ArticleTitle}原创论文样式
d return it as a byte array.
  private byte getBytes( String filename ) throws IOException {
    // Find out the length of the file
    File file = new File( filename );
    long len = file.length();
    // Create an array that''s just the right size for the file''s
    // contents
    byte raw = new byte[(int)len];
    // Open the file
    FileInputStream fin = new FileInputStream( file );
    // Read all of it into the array; if we don''t get all,
    // then it''s an error.
    int r = fin.read( raw );
    if (r != len)
      throw new IOException( "Can''t read all, "+r+" != "+len );
    // Don''t forget to close the file!
    fin.close();
    // And finally return the file contents as an array
    return raw;
  }
  // Spawn a process to compile the java source code file
  // specified in the ''javaFile'' parameter.  Return a true if
  // the compilation worked, false otherwise.
  private boolean compile( String javaFile ) throws IOException {
    // Let the user know what''s going on
    System.out.println( "CCL: Compiling "+javaFile+"" );
    // Start up the compiler
    Process p = Runtime.getRuntime().exec( "javac "+javaFile );
    // Wait for it to finish running
    try {
      p.waitFor();
    } catch( InterruptedException ie ) { System.out.println( ie ); }
    // Check the return code, in case of a compilation error
    int ret = p.exitValue();
    // Tell whether the compilation worked
    return ret==0;
  }
  // The heart of the ClassLoader -- automatically compile
  // source as necessary when looking for class files
  public Class loadClass( String name, boolean resolve )
      throws ClassNotFoundException {
    // Our goal is to get a Class object
    Class clas = null;
    // First, see if we''ve already dealt with this one
    clas = findLoadedClass( name );
    //System.out.println( "findLoadedClass: "+clas );
    // Create a pathname from the class name
    // E.g. java.lang.Object => java/lang/Object
    String fileStub = name.replace( ''.'', ''/'' );
    // Build objects pointing to the source code (.java) a

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号