- 浏览: 218701 次
- 性别:
- 来自: 天津
文章分类
最新评论
-
flychao88:
nothingismao 写道623deyingxiong 写 ...
nio与io的比较 -
李冰冰:
如果NIO单个线程处理业务逻辑,那么如果这个业务逻辑非常耗时, ...
nio与io的比较 -
wj_126mail:
IO是一个连接就创建一个线程来处理;NIO是一个线程在处理所有 ...
nio与io的比较 -
nothingismao:
623deyingxiong 写道wwj85523 写道 看完 ...
nio与io的比较 -
623deyingxiong:
wwj85523 写道
看完后我迷糊了,
IO一样可以一个线 ...
nio与io的比较
摘自:https://www6.software.ibm.com/developerworks/cn/education/java/j-classloader/tutorial/j-classloader-6-1.shtml实现的一个自动编译的classloader里面使用了很多我不熟悉的api。值得学习、借鉴
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; } }
- src.rar (3.7 KB)
- 下载次数: 15
发表评论
-
ant build.xml例子
2010-01-10 20:16 2512<?xml version="1.0" ... -
避免内存泄露专题
2009-12-17 16:19 1237For Java: 1.限制使用单例模式;单例模式是引发mem ... -
万恶的inner class, memory leak的源头之一
2009-12-17 15:35 1327非static inner class隐式的含有一个引用指向o ... -
resolve maven [Request to merge when 'filtering' is not identical]. issue
2009-11-06 11:18 2711mvn org.apache.maven.plugins:ma ... -
an example of cloneable
2009-10-10 09:43 1081The following code describes ho ... -
jvm 远程debug
2009-04-17 10:01 1150-Xdebug -Xrunjdwp:transport=dt_ ... -
java dynamic proxy
2008-11-21 20:45 1134package proxy.cxz.org; impor ... -
serializable例子一则
2008-11-15 21:13 1056实现Serializable接口,编写地定义的针对transi ... -
jmx添加naming service以及一个rmi 监听方式
2008-11-11 15:22 2634ObjectName namingName = new ... -
指定编码器、解码器,并且利用ByteBuffer读写文件
2008-11-04 20:48 2689指定编码器、解码器,并且利用ByteBuffer读写文件。 做 ... -
java 网络编程探讨
2008-11-02 19:30 2097毕业有一年多了,大学的时候上过网络课程,但我一直认为:网络技术 ... -
Differences Between notify() and notifyAll()
2008-08-08 21:20 1052package com.cxz.currency.test; ... -
一套基于http的聊天c/s结构工具(除了网页tomcat还能做什么)
2008-08-08 11:51 4050在我的认识当中以前一直有一种误区认为:tomcat=web。在 ... -
jdk1.6中添加的future
2008-07-18 13:59 1329摘自:http://caterpillar.onlyfun.n ... -
模拟jdk1.5中reentrantlock
2008-07-18 13:15 1199选自:java线程2e。 类似于jdk1.5中的reentra ... -
java线程2e中写得相当花哨的thread例子!
2008-07-14 16:53 1785太鲜灵儿了! package com.cxz.tools; ... -
j2se中实现jndi的控制、管理
2008-07-01 19:34 1173jndi例子一则,转载自网络。利用一个container实现了 ... -
jmx控制tomcat
2008-07-01 09:22 5050以前所作的一切应用程序基本上都是由tomcat容器,控制web ... -
jmx例子一则
2008-06-30 07:32 2895很简单的一个hellojmx的例子。其中实现了:标准bean、 ... -
nio与io的比较
2008-06-21 17:31 14949nio是new io的简称,从jdk1.4就被引入了。现在的j ...
相关推荐
CompileClassLoader.java
本文介绍了 Java 实现的自定义类加载器的原理和实现技巧,包括 ClassLoader 类、自定义类加载器的功能、CompileClassLoader 示例等。自定义类加载器可以满足特定的需求,例如执行代码前自动验证数字签名、根据用户...
该项目是一款基于freeRTOS操作系统和STM32F103x微控制器的手机远程控制浴室温度系统设计源码,共包含1087个文件,包括580个C语言源文件、269个头文件、45个汇编源文件、36个数据文件、36个目标文件、35个编译规则文件、28个包含文件、27个文本文件、6个源文件、3个归档文件。此系统通过手机远程实现对浴室温度的有效控制,适用于智能浴室环境管理。
labview程序代码参考学习使用,希望对你有所帮助。
labview程序代码参考学习使用,希望对你有所帮助。
labview程序代码参考学习使用,希望对你有所帮助。