当我使用javax.tools.JavaCompiler编译源代码时如何设置classpath? [英] How to set classpath when I use javax.tools.JavaCompiler compile the source?

查看:710
本文介绍了当我使用javax.tools.JavaCompiler编译源代码时如何设置classpath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用类 javax.tools.JavaCompiler (jdk6)来编译源文件,但源文件依赖于某些jar文件。如何设置 javax.tools.JavaCompiler的类路径

I use the class javax.tools.JavaCompiler (jdk6) to compile a source file, but the source file depends on some jar file. How to set the classpath of the javax.tools.JavaCompiler?

推荐答案

javax.tools.JavaCompiler#getTask()方法需要一个 options 允许设置编译器选项的参数。以下消息描述了一种设置它们以便访问调用程序的类路径:

The javax.tools.JavaCompiler#getTask() method takes an options parameter that allows to set compiler options. The following message describes an easy way to set them in order to access the calling program's classpath:


您需要配置标准的
java文件管理器以了解
jar文件( s) - 你使用编译器
options参数来做到这一点。

You need to configure the standard java file manager to know about the jar files(s) - you use the compiler options argument to do that.

默认情况下,java编译器对象
似乎只知道默认的
bootclasspath,extdirs
和endorseddirs目录的位置,其类路径为

By default the java compiler object only seems to know about the default locations for bootclasspath, extdirs and endorseddirs directories in terms of its classpath.

你需要添加调用程序的
java编译器的当前类路径
实例在
上传递标准文件管理器,然后在
中查找jar文件中的类。

这是我在我编写的编译器
包装器

List<String> optionList = new ArrayList<String>();
// set compiler's classpath to be same as the runtime's
optionList.addAll(Arrays.asList("-classpath",System.getProperty("java.class.path")));

// any other options you want
optionList.addAll(Arrays.asList(options));

JavaCompiler.CompilationTask task = compiler.getTask(out,jfm,diagnostics,optionList,null,jfos);


所有你需要的是获得正确的类路径集在运行调用程序时。

All you'll need then is to get the proper classpath set when running the calling program.

这篇关于当我使用javax.tools.JavaCompiler编译源代码时如何设置classpath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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