模拟IDE上的run过程

看了一下老陈写的模仿JDK动态代理,从中取一部分单独扩展,模拟一下IDE上的run过程(不愧是老陈,去年写的东西我要现在才能理解)

对run过程的猜想

在点击run的过程中应该做了不少事。先编译运行run工具,执行引擎增加一个线程开始执行被加载的run工具的字节码指令;线程执行过程中将目标源码进行编译,获取ClassLoader实例对字节码进行加载,在堆上创建并初始化Class

模拟工具实现run过程

import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.lang.reflect.Method;

/**
 * USER: zzzz76
 * Date: 2017/12/30
 * Time: 16:44
 */
public class beforeRun {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        try {
            String fileName = args[0];
            System.err.println("run target: " + fileName);

            //拿到编译器
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            //文件管理者
            StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
            //获取文件
            Iterable iterable = fileManager.getJavaFileObjects(fileName);
            //创建编译任务
            JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, iterable);
            //进行编译
            task.call();
            fileManager.close();
            System.err.println("************ compile successfully! ************");

            ClassLoader classLoader = ClassLoader.getSystemClassLoader();
            //加载目标字节码
            String className = fileName.substring(0, fileName.length() - 5);
            Class targetClass = classLoader.loadClass(className);
            //调用main方法
            Method method = targetClass.getDeclaredMethod("main", String[].class);
            method.invoke(null, (Object) new String[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

/**
 * USER: zzzz76
 * Date: 2017/12/30
 * Time: 17:28
 */
public class Target {
    public static void main(String[] args) {
        System.out.println("run target!");
        Other.runOther();
    }
}

/**
 * USER: zzzz76
 * Date: 2017/12/30
 * Time: 18:14
 */
public class Other {
    public static void runOther() {
        System.out.println("run other!");
    }
}

运行结果

?  Desktop javac beforeRun.java
?  Desktop java beforeRun Target.java
run target: Target.java
************ compile successfully! ************
run target!
run other!

原文地址:https://www.cnblogs.com/zzzz76/p/8150862.html

时间: 2024-08-05 14:23:20

模拟IDE上的run过程的相关文章

Samza在YARN上的启动过程 =》 之一

运行脚本,提交job 往YARN提交Samza job要使用run-job.sh这个脚本. samza-example/target/bin/run-job.sh  --config-factory=samza.config.factories.PropertiesConfigFactory  --config-path=file://$PWD/config/hello-world.properties 这脚本的内容是什么呢? exec $(dirname $0)/run-class.sh or

ucos-ii在ti dsp 28377芯片上的运行过程和移植过程

1.移植过程 在将ucos移植到28377d平台上时主要遇见了下面几个问题, 1) 文件怎么组织,是通过修改micrim上提供的28335一直代码修改而成的,下载地址为:https://www.micrium.com/. 2)移植完成后发现创建任务完成后,任务无法跳转,移植在主函数中来回循环 3)当使用ostimedly()函数对任务延时,当延时时间已经完成,系统无法跳出空任务循环,移植在IdleTask中运行 4)任务切换过程中总是跳入到异常中断中. 移植思路: 开始移植过程时,下载了micr

Fiddler2 模拟文件上传

最近遇到一个需求,需要上传音频文件, 服务端使用webService 通过spring3 进行文件上传.代码完成后使用 html 通过post 方式请求接口成功了,但不知道如何使用Fiddler2工具进行模拟测试,经过一番努力终于成功了,在此分享一下! spring3文件上传开以参考:http://blog.csdn.net/maotongbin/article/details/11921329 1.打开Fiddler2工具,填写好必要的信息 2.点击UPload file 按钮上传文件 3.这

手动模拟attach cinder volume的过程

我们首先启动一台机器,启动的时候attach一个volume 创建一个空的cinder volume root:~# cinder create --display-name emptyvolume11g 11+---------------------+--------------------------------------+|       Property      |                Value                 |+--------------------

ORB_SLAM2在Android上的移植过程

作者:Frank 转载请注明出处 一直没时间写博客,最近抽时间写了些关于在ORB_SLAM2在Android上的移植过程,也算是点经验吧. 写完后一个手贱点了个链接,瞬间1/3工作量没了,深夜弄完也是醉了... 正文开始 这篇博客讲述如何在Android平台上移植ORB_SLAM2,讲述过程包括基本的Android环境的搭建和NDK环境的配置,Android下移植的基本概念,ORB的具体移植步骤等. Android平台搭建和NDK环境配置 系统:windows7 32bit IDE:Eclips

用两个队列模拟实现一个栈的过程

栈具有"后进先出"的特点,即某个元素最后进入栈,却最先出栈:队列具有"先进先出"的特点,即元素从队尾依次进队列,依次从队头出队列:现在用两个队列模拟实现一个栈的过程,详细过程请看下面这张本人制作的gif图: 实现代码: #include <iostream> using namespace std; #include <queue> template <typename T> class Stack { public: void

vb.net模拟文件上传

Imports Microsoft.VisualBasic Imports System.Net Imports System.Security.Cryptography.X509Certificates Imports System.Security.Authentication Imports System.Net.Security Imports System.Collections.Generic Imports System.Text Imports System.IO Public 

Samza在YARN上的启动过程 =》 之二 submitApplication

首先,来看怎么构造一个org.apache.hadoop.yarn.client.api.YarnClient ? 1 2 3 4 5 class ClientHelper(conf: Configuration) extends Logging {   val yarnClient = YarnClient.createYarnClient   info("trying to connect to RM %s" format conf.get(YarnConfiguration.RM

GitHub初步探索-1-使用本地代码仓库,简化上传的过程

使用GitHub对于我们写Java的同志们来说是一个非常好的代码存储的方式,但是因为是全英文的,操作起来有一点复杂,所以我不是经常使用 ,但是最近代码越敲越多,再加上老师要求,希望使用比较简单的方法来上传代码到github,在宿舍大神的帮助下,学会了使用本地github来进行代 码的上传,然后我有参考了下网上的使用教程,大概阐述一下我的上传经验. 首先,我参考了http://www.cnblogs.com/foreveryt/p/4077380.html  博主写的代码,这里介绍了github的