`

CompilingClassLoader

    博客分类:
  • java
阅读更多
import java.io.*;   
  
/*  
 
A CompilingClassLoader compiles your Java source on-the-fly.  It checks  
for nonexistent .class files, or .class files that are older than their  
corresponding source code.  
 
*/  
  
public class CompilingClassLoader extends ClassLoader   
{   
  // Given a filename, read the entirety of that file from disk   
  // and 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) and object   
    // code (.class)   
    String javaFilename = fileStub+".java";   
    String classFilename = fileStub+".class";   
  
    File javaFile = new File( javaFilename );   
    File classFile = new File( classFilename );   
  
    //System.out.println( "j "+javaFile.lastModified()+" c "+   
    //  classFile.lastModified() );   
  
    // First, see if we want to try compiling.  We do if (a) there   
    // is source code, and either (b0) there is no object code,   
    // or (b1) there is object code, but it's older than the source   
    if (javaFile.exists() &&   
         (!classFile.exists() ||   
          javaFile.lastModified() > classFile.lastModified())) {   
  
      try {   
        // Try to compile it.  If this doesn't work, then   
        // we must declare failure.  (It's not good enough to use   
        // and already-existing, but out-of-date, classfile)   
        if (!compile( javaFilename ) || !classFile.exists()) {   
          throw new ClassNotFoundException( "Compile failed: "+javaFilename );   
        }   
      } catch( IOException ie ) {   
  
        // Another place where we might come to if we fail   
        // to compile   
        throw new ClassNotFoundException( ie.toString() );   
      }   
    }   
  
    // Let's try to load up the raw bytes, assuming they were   
    // properly compiled, or didn't need to be compiled   
    try {   
  
      // read the bytes   
      byte raw[] = getBytes( classFilename );   
  
      // try to turn them into a class   
      clas = defineClass( name, raw, 0, raw.length );   
    } catch( IOException ie ) {   
      // This is not a failure!  If we reach here, it might   
      // mean that we are dealing with a class in a library,   
      // such as java.lang.Object   
    }   
  
    //System.out.println( "defineClass: "+clas );   
  
    // Maybe the class is in a library -- try loading   
    // the normal way   
    if (clas==null) {   
      clas = findSystemClass( name );   
    }   
  
    //System.out.println( "findSystemClass: "+clas );   
  
    // Resolve the class, if any, but only if the "resolve"   
    // flag is set to true   
    if (resolve && clas != null)   
      resolveClass( clas );   
  
    // If we still don't have a class, it's an error   
    if (clas == null)   
      throw new ClassNotFoundException( name );   
  
    // Otherwise, return the class   
    return clas;   
  }   
}  
分享到:
评论

相关推荐

    infrared-remote-candroid studiodemo

    android studio下载

    【新质生产力】新质生产力赋能智能制造数字化解决方案.pptx

    【新质生产力】新质生产力赋能智能制造数字化解决方案.pptx

    基于matlab实现的用于应用布格重力异常数据反演地下异常密度体.rar

    基于matlab实现的用于应用布格重力异常数据反演地下异常密度体.rar

    node-v8.10.0-linux-x64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    基于Yolov5目标检测和deepsort目标跟踪无人机跟踪.zip

    无人机最强算法源码,易于部署和学习交流使用

    数据库课程设计实战.zip

    数据库课程设计后端 使用Springboot + Mybatis + Redis + Maven 数据库课程设计实战.zip,使用到了所有的相关SQL 的操作,如增删改查等,让你可以在一个项目里面,锻炼到所有的数据库相关的知识。项目亲测可以运行,里面含有运行相关的文档,不会的可以丝我请求帮助。 数据库课程设计后端 使用Springboot + Mybatis + Redis + Maven 具体的表和相关的数据如下: 用户(电话号码,密码,身份证号,邮箱,真实姓名,用户类型,性别,地址) 乘客(用户电话号码,乘客身份证号,乘客真实姓名,乘客电话号码,乘客类型,地址) 列车信息(列车编号,车次,列车类型,列车车厢数,列车始发站,列车终点站,列车开车时间,列车到达时间,列车到达日期,列车运行时间,列车状态) 列车座位信息(列车编号,车厢号,座位类型,座位数) 列车经停信息(列车编号,车次,车站编号,车站名,到达时间,总运行时间,开车时间) 订单信息(订单编号,用户电话号码,乘客身份证号码,列车编号,出发站编号,到达站编号,车厢号,座位编号,订单创建时间,订单状态,开车时间)

    咨询的分析方法gl.ppt

    咨询的分析方法gl.ppt

    node-v10.14.0-linux-ppc64le.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    2019年电赛无人机题目(B题)OpenMV相关代码

    These're the OpenMV codes written by microPython in 2019 NUEDC. 2019年电赛无人机题目(B题)OpenMV相关代码(原创).zip

    无人机降落TRT版本.zip

    无人机最强算法源码,易于部署和学习交流使用

    熊出没.zip

    熊出没.zip

    基于SpringBoot和Vue的家教信息平台设计与实现.zip

    基于SpringBoot和Vue的家教信息平台设计与实现.zip 有完整的部署指导文档,源码也是完整的,可以直接运行,里面包含了所有的相关步骤。 本文旨在设计和实现一套基于Java技术的家教信息系统,采用Spring Boot框架构建后端服务,MySQL数据库存储数据,Vue.js作为前端框架实现用户界面。该系统旨在解决家教信息管理的问题,包括家教师资信息管理、用户信息管理以及家教入驻等功能。通过综合运用Java、Spring Boot、MySQL和Vue等技术,实现了系统的高效运行和良好的用户体验。系统提供了用户注册、登录、信息查看和编辑等功能,同时支持家教的发布和查看,用户信息的管理以及家教审核的后台管理。家长可以方便地寻找合适的家教老师,家教老师也能够更便捷地管理自己的信息和相关资料。通过本设计,展示了Java技术在现代化家教信息系统中的应用,为家教行业的信息化管理提供了一种有效的解决方案。该系统的设计与实现将为家长、家教老师和用户提供便利,促进家教行业的发展与进步。 关键词:SpringBoot; MySQL; 系统设计; 家教

    利用CNN进行无人售货机的商品识别.zip

    无人机最强算法源码,易于部署和学习交流使用

    node-v11.10.1-linux-armv6l.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    (R语言)-6-箱线图的绘制

    (R语言)-6-箱线图的绘制

    麦肯锡-xx联通固定市场举措gl.ppt

    麦肯锡-xx联通固定市场举措gl.ppt

    在PyCharm中配置Python环境步骤

    附件是在PyCharm中配置Python环境步骤,文件绿色安全,请大家放心下载,仅供交流学习使用,无任何商业目的!

    【北京工业大学】集成电路分析与设计实验报告

    本课程实验分为数字集成电路设计实验与全定制设计实验两部分。 实验1—4为基于Cadence的数字集成电路设计实验部分,主要内容为通过一个简单数字低通滤波器的设计、综合、仿真,让学生熟悉数字集成电路前段实际设计流程,以培养学生实际设计集成电路的能力。具体为:实验1Matlab实现数字低通滤波器算法设计。 实验2Linux环境下基本操作。 实验3RTLCompiler对数字低通滤波器电路的综合。 实验4NC对数字低通滤波器电路的仿真。 其中,实验1主要目的是为了展示算法分析的方法和重要性。使用Matlab实现数字滤波器的算法设计和HDL代码生成。由于Matlab工具可以在Windows环境下工作,而其他集成电路EDA工具均需要在linux下工作,故建议本实验在课堂演示和讲述,学生课下练习。实验2的主要目的是学习linux下的基本操作。包括目录管理、文件管理、文件编辑以及文件压缩等在使用集成电路EDA工具时所需要的操作。本实验是实验3和实验4的基础,建议在实验室完成。

Global site tag (gtag.js) - Google Analytics