编写java脚本:从外部文件导入类
我想导入一个我已经在外部文件夹中编写的类,
例如 :
我的类 Example.java
位于 c:\class\Example.java
中,与我的脚本类似,
var importedClass = new JavaImporter("c:\\class\\Example.java");
或者
importClass("c:\\class\\Example.java");
位于 ScriptEngine rhino 的脚本中
我怎样才能做到这一点???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道您想要:
javax.tools 包提供了一种编译代码的机制,但如果您不是在 JDK 中运行,ToolProvider.getSystemJavaCompiler() 将返回
null
并且您必须依赖一些其他编译机制(调用外部编译器;嵌入 Eclipse 编译器;等等)。Java 字节码(
.class
二进制文件)可以在运行时通过 类加载器。为了使加载的类对脚本引擎可见,您需要通过 ScriptEngineManager(ClassLoader) 构造函数。
编辑:根据要求
此脚本只是调用Java反射API来加载并实例化
C:\foo\bin
目录中的类HelloWorld.class
:还有更多我确信这是一种优雅的方式。
I understand that you want to:
The javax.tools package provides a mechanism for compiling code, though if you're not running in a JDK, ToolProvider.getSystemJavaCompiler() will return
null
and you'll have to rely on some other compilation mechanism (invoking an external compiler; embedding the Eclipse compiler; etc.).Java bytecode (
.class
binaries) can be loaded at runtime via ClassLoaders.In order for the loaded classes to be visible to your scripting engine, you'll need to provide them via the ScriptEngineManager(ClassLoader) constructor.
EDIT: based on the requirements
This script just invokes the Java reflection API to load and instantiate a class
HelloWorld.class
from theC:\foo\bin
directory:There are more elegant ways of doing this, I'm sure.
我会质疑为什么要这样做。
此处列出的解决方案将起作用。问题是:
带有反射的解决方案
很难排除故障。
修补代码加载于
运行时?我工作过的每一个地方
不是。
I would question why do this.
The solutions listed here will work. The problem is going to be that:
solution with reflection that will
be hard to troubleshoot.
patching code that is loaded at
Runtime ? Everyplace I have worked
at is not.
如果我理解正确的话,您实际上想要做的是加载 Java 类,以便您可以(大概)创建实例等。术语是动态加载而不是导入。
Java 允许您使用
ClassLoader.loadClass(String)
方法动态加载字节码文件 (*.class
)。关于这个主题有很多资源;例如 JNDI 中的“类加载”页面教程。准备好花一些时间来了解这个主题。特别是,由于您尝试加载不在应用程序的正常类路径上的类,因此您需要创建一个新的类加载器来执行此操作。java.lang.ClassLoader
类的 Javadoc 为 这里。Java源代码不能直接加载,必须首先使用Java编译器进行编译。如果您使用的是现代 JDK 安装,则可以在运行时调用 Java 编译器。但 JRE 安装不包括 Java 编译器。如果您的平台在运行时有可用的 Java 编译器,则可以通过
ToolProvider
类。再次强调,从正在运行的 Java 应用程序中调用 Java 编译器是很复杂的。If I understand you correctly, what you are actually trying to do is load Java classes so that you can (presumably) create instances, etcetera. The term for this is dynamic loading not importing.
Java allows you to dynamically load bytecode files (
*.class
) using theClassLoader.loadClass(String)
method. There are lots of resources on this topic; e.g. the "Class Loading" page from the JNDI tutorial. Be prepared to spend some time getting your head around this topic. In particular, since you are trying to load a class that is not on your application's normal classpath, you will need to create a new classloader to do this. The Javadocs for thejava.lang.ClassLoader
class are here.Java source code cannot be directly loaded, but must first be compiled using a Java compiler. If you are using a modern JDK installation, it is possible to call the Java compiler at runtime. But a JRE installation does not include a Java compiler. If your platform has a Java compiler available at runtime, you can access it via the
getSystemJavaCompiler()
static method of theToolProvider
class. Once again, calling the Java compiler from within a running Java application is complicated.