加载外部源代码并在内部使用它们(通过重新编译或其他东西) [英] Loading external source code and using them internally (by re-compiling or something)

查看:127
本文介绍了加载外部源代码并在内部使用它们(通过重新编译或其他东西)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用外部存储的源代码并将其加载到Java程序中,以便它可以被它使用?



我想有一个程序可以在不编辑完整源代码的情况下进行更改,甚至可以在不编译每次的情况下进行更改。另一个优点是,我可以像我想要的那样更改部分代码。



当然我必须有接口,以便可以将数据发送到此并获取它再次回到固定源程序。



当然它应该比纯解释系统更快。



<那么有没有办法这样做,就像这些外部源代码部分的额外编译和程序启动完成后一样?



提前谢谢你,Andreas:)

解决方案

你需要 javax.tools API 。因此,您需要至少安装JDK才能使其工作(并让您的IDE指向它而不是JRE)。这是一个基本的启动示例(没有适当的异常和编码处理只是为了使基本示例不那么透明,咳嗽):

  public static void main(String ... args)throws Exception {
String source =public class Test {static {System.out.println(\test \);}} ;

文件root = new文件(/ test);
文件sourceFile = new File(root,Test.java);
Writer writer = new FileWriter(sourceFile);
writer.write(source);
writer.close();

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null,null,null,sourceFile.getPath());

URLClassLoader classLoader = URLClassLoader.newInstance(new URL [] {root.toURI()。toURL()});
Class<?> cls = Class.forName(Test,true,classLoader);
}

这应该打印 test 在stdout中,由测试源代码中的静态初始化程序完成。如果这些类实现了已经在类路径中的某个接口,那么进一步使用会更容易。否则,您需要参与 Reflection API 来访问和调用方法/字段。


Is there a way in using externally stored sourcecode and loading it into a Java programm, so that it can be used by it?

I would like to have a program that can be altered without editing the complete source code and that this is even possible without compiling this every time. Another advantage is, that I can change parts of the code like I want.

Of course I have to have interfaces so that it is possible to send data into this and getting it back into the fixed source program again.

And of course it should be faster than a pure interpreting system.

So is there a way in doing this like an additional compiling of these external source code parts and a start of the programm after this is done?

Thank you in advance, Andreas :)

解决方案

You need the javax.tools API for this. Thus, you need to have at least the JDK installed to get it to work (and let your IDE point to it instead of the JRE). Here's a basic kickoff example (without proper exception and encoding handling just to make the basic example less opaque, cough):

public static void main(String... args) throws Exception {
    String source = "public class Test { static { System.out.println(\"test\"); } }";

    File root = new File("/test");
    File sourceFile = new File(root, "Test.java");
    Writer writer = new FileWriter(sourceFile);
    writer.write(source);
    writer.close();

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    compiler.run(null, null, null, sourceFile.getPath());

    URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
    Class<?> cls = Class.forName("Test", true, classLoader);
}

This should print test in stdout, as done by the static initializer in the test source code. Further use would be more easy if those classes implements a certain interface which is already in the classpath. Otherwise you need to involve the Reflection API to access and invoke the methods/fields.

这篇关于加载外部源代码并在内部使用它们(通过重新编译或其他东西)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆