java 使用 javax.tools.JavaCompiler 编译源码时如何设置类路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1563909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:06:33  来源:igfitidea点击:

How to set classpath when I use javax.tools.JavaCompiler compile the source?

javajdk1.6java-compiler-apijsr199

提问by Diablo.Wu

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(jdk6)来编译源文件,但是源文件依赖于一些 jar 文件。如何设置类路径javax.tools.JavaCompiler

采纳答案by Pascal Thivent

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

所述javax.tools.JavaCompiler#getTask()方法把一个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.

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.

You need to add the calling program's current classpath to the java compiler instance's which gets passed on the the standard file manager, which will then find classes in the jar files.

Here's how I do it in the compiler wrapper I wrote

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);

您需要配置标准 java 文件管理器以了解 jar 文件 - 您使用编译器选项参数来执行此操作。

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

您需要将调用程序的当前类路径添加到传递给标准文件管理器的 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.

您需要做的就是在运行调用程序时设置正确的类路径。

回答by Wenlin.Wu

The same problem occurred to me recently, finally I found two workarounds. You can set the class path either by invoke StandardJavaFileManager.setLocation(StandardLocation.CLASS_PATH, "YOUR_CLASS_PATH")or Compiler.getTask(ARG_0, ARG_1, ARG_2, CLASS_PATH_OPTIONS, just as the first answer posted here says.

最近我遇到了同样的问题,最后我找到了两个解决方法。您可以通过调用StandardJavaFileManager.setLocation(StandardLocation.CLASS_PATH, "YOUR_CLASS_PATH")或 Compiler.getTask(ARG_0, ARG_1, ARG_2, CLASS_PATH_OPTIONS来设置类路径,正如此处发布的第一个答案所说。