如何从 Eclipse 运行 Javac
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5987694/
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
How to run Javac from Eclipse
提问by Viktor Apoyan
I'm trying to run 'javac' tool on a compiled .class file in Eclipse. I open External Tools Configurationthem fill the filds:
我正在尝试在 Eclipse 中编译的 .class 文件上运行“javac”工具。我打开外部工具配置它们填充字段:
Location:C:\Program Files\Java\jdk1.6.0_25\bin\javac.exe
位置:C:\Program Files\Java\jdk1.6.0_25\bin\javac.exe
Working directory:${workspace_loc:/Main/bin}
工作目录:${workspace_loc:/Main/bin}
Arguments:?
参数:?
I want to ask you what must I write in the Argumentsfield, and am I fill*Location* and Working directory:fields right ?
我想问你我必须在Arguments字段中写什么,我是否填写了 * Location* 和Working directory:字段,对吗?
回答by Balder
Launching the javac compiler from within Eclipse can be a very useful feature in some cases (e.g. for testing purposes, to compare the javac output with the output of the Eclipse compiler, to recompile individual class files with special javac compiler options or with a compiler of a different JDK version etc.). Other than using ant, there are two convenient ways to integrate javac into Eclipse: Setting up an "External Tools Configuration" for javac, or adding javac to the Eclipse build chain of a project.
在某些情况下,从 Eclipse 中启动 javac 编译器可能是一个非常有用的功能(例如,出于测试目的,将 javac 输出与 Eclipse 编译器的输出进行比较,使用特殊的 javac 编译器选项或使用不同的 JDK 版本等)。除了使用ant 之外,还有两种方便的方法可以将 javac 集成到 Eclipse 中:为 javac 设置“外部工具配置”,或者将 javac 添加到项目的 Eclipse 构建链中。
Setting up an "External Tools Configuration" for javac
为 javac 设置“外部工具配置”
Here are the steps required to setup the javac compiler so it can be used inside Eclipse (ready to use launch configurations are below):
以下是设置 javac 编译器所需的步骤,以便它可以在 Eclipse 中使用(准备使用启动配置如下):
- Create a new External Tools Configuration.
- Set the "Location" of the configuration to the path of the javac executable (e.g.
C:\Program Files (x86)\Java\jdk1.7.0_25\bin\javac.exe
). - Set the "-classpath" option of javac to the classpath of the project inside the "Arguments" field of the configuration (e.g.
-classpath ${project_classpath}
). - Set the "-d" option of javac to the binary folder of the project (e.g.
-d ${project_loc}\bin
). - Add any additional javac optionslike "-verbose" or "-parameters" to the argument list.
- Add the path to the source file or source folder to the endof the argument list (e.g.
${selected_resource_loc}
for the selected source file or${selected_resource_loc}\*
for the selected package). The complete "Arguments" field for the configuration could look like this:-verbose -classpath ${project_classpath} -d ${project_loc}\bin ${selected_resource_loc}\*
- Run the External Tool Configurationon the selected file.
- 创建一个新的外部工具配置。
- 将配置的“位置”设置为 javac 可执行文件的路径(例如
C:\Program Files (x86)\Java\jdk1.7.0_25\bin\javac.exe
)。 - 将 javac 的“-classpath”选项设置为配置的“Arguments”字段内项目的类路径(例如
-classpath ${project_classpath}
)。 - 将 javac 的“-d”选项设置为项目的二进制文件夹(例如
-d ${project_loc}\bin
)。 - 将任何其他 javac 选项(如“-verbose”或“-parameters”)添加到参数列表中。
- 将源文件或源文件夹的路径添加到参数列表的末尾(例如
${selected_resource_loc}
,对于选定的源文件或${selected_resource_loc}\*
选定的包)。配置的完整“参数”字段可能如下所示:-verbose -classpath ${project_classpath} -d ${project_loc}\bin ${selected_resource_loc}\*
- 在所选文件上运行外部工具配置。
In addition to that, you will probably want to select "Refresh resources upon completion" for the selected project under the "Refresh" tab of the tool configuration, and possibly deselect "Build before launch" under the "Build" tab.
除此之外,您可能希望在工具配置的“刷新”选项卡下为所选项目选择“完成时刷新资源”,并可能在“构建”选项卡下取消选择“启动前构建”。
I created two default launch configurations for javac, that you may reuse by putting them into a file ending with ".launch" in your project folder (e.g. "javac.launch"). Eclipse will automatically detect these configuration files once you open the "External Tools Configuration" dialog. You will most likely need to change the location of javac to the location of javac on your computer.
我为 javac 创建了两个默认启动配置,您可以通过将它们放入项目文件夹中以“.launch”结尾的文件(例如“javac.launch”)中来重用它们。打开“外部工具配置”对话框后,Eclipse 将自动检测这些配置文件。您很可能需要将 javac 的位置更改为您计算机上 javac 的位置。
File "javac (verbose file).launch"- launches javac with the -verbose option on a single selected file:
文件 "javac (verbose file).launch"- 在单个选定文件上使用 -verbose 选项启动 javac:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramLaunchConfigurationType">
<stringAttribute key="org.eclipse.debug.core.ATTR_REFRESH_SCOPE" value="${project}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LAUNCH_CONFIGURATION_BUILD_SCOPE" value="${none}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="C:\Program Files (x86)\Java\jdk1.8.0\bin\javac.exe"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS" value=" -verbose -classpath ${project_classpath} -d ${project_loc}\bin ${selected_resource_loc}"/>
</launchConfiguration>
File "javac (dir).launch"- launches javac on the selected package:
文件“javac (dir).launch”- 在选定的包上启动 javac:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramLaunchConfigurationType">
<stringAttribute key="org.eclipse.debug.core.ATTR_REFRESH_SCOPE" value="${project}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LAUNCH_CONFIGURATION_BUILD_SCOPE" value="${none}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="C:\Program Files (x86)\Java\jdk1.8.0\bin\javac.exe"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS" value="-classpath ${project_classpath} -d ${project_loc}\bin ${selected_resource_loc}\*"/>
</launchConfiguration>
Adding javac to the Eclipse build chain of a project
将 javac 添加到项目的 Eclipse 构建链中
Adding javac to the build chain, so it gets executed automatically during a full or automatic build is done in a similar way as described above:
将 javac 添加到构建链中,以便它在完整或自动构建期间自动执行,其方式与上述类似:
- Open the project properties and select the "Builders" tab. Then, if you already have a launch configuration for javac (see above), you can "Import..." it as a new builder. Otherwise you can create a "New..." builder for javac.
- You will most likely want to change the arguments for javac to fit your needs. First you should change the "-d" argument to the binary folder of the built project:
-d ${build_project}\bin
. Then you should add the source files/folders that you want to be compiled by javac to the end of the argument list using the "${resource_loc}" variable. The complete argument list for compiling a single source file could look like this:-classpath ${project_classpath} -d ${build_project}\bin ${resource_loc:MyProject/src/myPackage/MyClass.java}
. To compile a complete package you can write${resource_loc:MyProject/src/myPackage}\*
instead. - Select the "Build Options" tab to configure, when the javac builder should be run. You will probably want to deselect "After a Clean".
- If you just want to add javac as an aditional compiler on top of the JDT compiler for some source files, then you are done. If you completely want to replace the JDT compiler, you must deselect the "Java Builder" from the "Builders" tab and will probably want to add a new tool to the build chain, that performs the clean operation of the built (otherwise the class files will not get deleted during a built).
- 打开项目属性并选择“Builders”选项卡。然后,如果您已经有 javac 的启动配置(见上文),您可以“导入...”它作为一个新的构建器。否则,您可以为 javac 创建一个“新建...”构建器。
- 您很可能希望更改 javac 的参数以满足您的需要。首先,您应该将“-d”参数更改为已构建项目的二进制文件夹:
-d ${build_project}\bin
. 然后,您应该使用“${resource_loc}”变量将要由 javac 编译的源文件/文件夹添加到参数列表的末尾。编译一个源文件的完整参数列表看起来是这样的:-classpath ${project_classpath} -d ${build_project}\bin ${resource_loc:MyProject/src/myPackage/MyClass.java}
。要编译一个完整的包,您可以${resource_loc:MyProject/src/myPackage}\*
改为编写。 - 选择“构建选项”选项卡进行配置,何时运行 javac 构建器。您可能想要取消选择“清洁后”。
- 如果您只想为某些源文件在 JDT 编译器之上添加 javac 作为附加编译器,那么您就完成了。如果您完全想要替换 JDT 编译器,您必须从“构建器”选项卡中取消选择“Java 构建器”,并且可能想要向构建链添加一个新工具,该工具执行构建的清理操作(否则类文件不会在构建过程中被删除)。
回答by Ken Chan
Creating a Java application launch configuration
Inside the Run Configuration ---> Main tab , you can specify the Main class to run
在 Run Configuration ---> Main tab 中,您可以指定要运行的 Main 类
Inside the Run Configuration ---> Arguments tab , you can specify the arguments passed into the main class. Simply enter your input arguments here and each argument is separated by space (eg arg1 arg2 arg3
will pass these 3 arguments to your main class) . You can also parameterize the arguments by using eclipse predefined variables folder_prompt
, file_prompt
or string_prompt
(You can use the "variables..." button inside the arguments tab to help you to configure them) . When you run the main class, Eclipse will then prompt the dialogs to let you enter the arguments
在 Run Configuration ---> Arguments 选项卡中,您可以指定传递给主类的参数。只需在此处输入您的输入参数,每个参数用空格分隔(例如arg1 arg2 arg3
将这 3 个参数传递给您的主类)。您还可以使用 eclipse 预定义变量来参数化参数folder_prompt
,file_prompt
或者string_prompt
(您可以使用参数选项卡内的“变量...”按钮来帮助您配置它们)。当您运行主类时,Eclipse 将提示对话框让您输入参数
Inside the Run Configuration ---> JRE tab ,you can specify the JRE to execute the main class.
在 Run Configuration ---> JRE 选项卡中,您可以指定 JRE 来执行主类。
回答by Mike Kucera
Use Ant.
使用蚂蚁。
Create an Ant buildfile that uses the javac task and run it using the eclipse Ant view.
创建一个使用 javac 任务的 Ant 构建文件并使用 eclipse Ant 视图运行它。