如何从Eclipse运行Javac [英] How to run Javac from Eclipse

查看:95
本文介绍了如何从Eclipse运行Javac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Eclipse中编译的.class文件上运行javac工具。我打开外部工具配置,填写文件:



位置: C:\Program Files\Java \jdk1.6.0_25\bin\javac.exe



工作目录: $ {workspace_loc:/ Main / bin}



参数



我想问你, strong>参数字段,我是否填写* 位置 *和工作目录栏位?

解决方案

在某些情况下,从Eclipse中启动javac编译器可能是非常有用的功能(例如,为了测试目的,将javac输出与Eclipse编译器的输出进行比较,以重新编译个人具有特殊javac编译器选项的类文件或与不同JDK版本的编译器等)。除了使用 ant ,将javac集成到Eclipse中有两种方便的方法:为javac设置外部工具配置,或者将javac添加到项目的Eclipse构建链中。



为javac设置外部工具配置



以下是设置javac编译器所需的步骤,可以在Eclipse中使用(准备使用启动配置如下):


  1. 创建一个新的外部工具配置

  2. 将配置的位置设置为javac可执行文件的路径(例如 C:\Program Files(x86)\Java\jdk1.7.0_25\ bin \javac.exe )。

  3. Se将javac的-classpath选项添加到配置的参数(Arguments)字段中的项目类路径中。
  4. 将javac的-d选项设置为项目的二进制文件夹(例如, -classpath $ {project_classpath} -d $ {project_loc} \bin )。

  5. 添加任何附加的javac选项,如-verbose或-parameters到参数列表。 li>
  6. 将源文件或源文件夹的路径添加到参数列表的 end 中(例如 $ {selected_resource_loc} 用于选定的源文件或 $ {selected_resource_loc} \ * 用于选定的包)。配置的完整参数字段可能如下所示: -verbose -classpath $ {project_classpath} -d $ {project_loc} \bin $ {selected_resource_loc} \ *

  7. 在所选文件上运行外部工具配置

此外,您可能需要在工具配置的刷新选项卡下选择为完成时刷新资源,并可能在构建选项卡下取消选择启动前生成。



我为javac创建了两个默认启动配置,您可以将它们重新使用到项目文件夹中的.launch结尾的文件中(例如javac.launch)。打开外部工具配置对话框后,Eclipse会自动检测这些配置文件。您最有可能需要将javac的位置更改为计算机上的javac位置。



文件javac(verbose file).launch - 在单个选定的文件上使用-verbose选项启动javac:

 <?xml version =1.0encoding =UTF-8standalone =否?> 
< launchConfiguration type =org.eclipse.ui.externaltools.ProgramLaunchConfigurationType>
< stringAttribute key =org.eclipse.debug.core.ATTR_REFRESH_SCOPEvalue =$ {project}/>
< stringAttribute key =org.eclipse.ui.externaltools.ATTR_LAUNCH_CONFIGURATION_BUILD_SCOPEvalue =$ {none}/>
< stringAttribute key =org.eclipse.ui.externaltools.ATTR_LOCATIONvalue =C:\Program Files(x86)\Java\jdk1.8.0\bin\javac.exe/ >
< stringAttribute key =org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTSvalue =-verbose -classpath $ {project_classpath} -d $ {project_loc} \bin $ {selected_resource_loc}/>
< / launchConfiguration>

文件javac(dir).launch - 启动javac选择包:

 <?xml version =1.0encoding =UTF-8standalone =no?> ; 
< launchConfiguration type =org.eclipse.ui.externaltools.ProgramLaunchConfigurationType>
< stringAttribute key =org.eclipse.debug.core.ATTR_REFRESH_SCOPEvalue =$ {project}/>
< stringAttribute key =org.eclipse.ui.externaltools.ATTR_LAUNCH_CONFIGURATION_BUILD_SCOPEvalue =$ {none}/>
< stringAttribute key =org.eclipse.ui.externaltools.ATTR_LOCATIONvalue =C:\Program Files(x86)\Java\jdk1.8.0\bin\javac.exe/ >
< stringAttribute key =org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTSvalue = - classpath $ {project_classpath} -d $ {project_loc} \bin $ {selected_resource_loc} \ */>
< / launchConfiguration>

将javac添加到项目的Eclipse构建链



将javac添加到构建链中,因此在完全或自动构建期间自动执行的javac以与上述相似的方式完成:


  1. 打开项目属性并选择Builders选项卡。然后,如果您已经有javac的启动配置(见上文),则可以将其导入...作为新的构建器。否则,您可以为javac创建一个新建...构建器。

  2. 您最有可能要更改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} \ *

  3. 选择构建选项选项卡,以便在运行javac构建器时进行配置。您可能要取消选择清理后。

  4. 如果您只想在某些源文件的JDT编译器之上添加javac作为附加编译器,那么就完成了。如果您完全想要替换JDT编译器,则必须从构建器选项卡中取消选择Java Builder,并且可能需要为构建链添加一个新工具,该构建链执行内置的清理操作(否则为类文件将不会被删除内置)。


I'm trying to run 'javac' tool on a compiled .class file in Eclipse. I open External Tools Configuration them fill the filds:

Location: C:\Program Files\Java\jdk1.6.0_25\bin\javac.exe

Working directory: ${workspace_loc:/Main/bin}

Arguments: ?

I want to ask you what must I write in the Arguments field, and am I fill*Location* and Working directory: fields right ?

解决方案

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.

Setting up an "External Tools Configuration" for 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):

  1. Create a new External Tools Configuration.
  2. 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).
  3. Set the "-classpath" option of javac to the classpath of the project inside the "Arguments" field of the configuration (e.g. -classpath ${project_classpath}).
  4. Set the "-d" option of javac to the binary folder of the project (e.g. -d ${project_loc}\bin).
  5. Add any additional javac options like "-verbose" or "-parameters" to the argument list.
  6. Add the path to the source file or source folder to the end of 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}\*
  7. Run the External Tool Configuration on the selected file.

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.

File "javac (verbose file).launch" - launches javac with the -verbose option on a single selected file:

<?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:

<?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

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:

  1. 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.
  2. 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.
  3. Select the "Build Options" tab to configure, when the javac builder should be run. You will probably want to deselect "After a Clean".
  4. 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).

这篇关于如何从Eclipse运行Javac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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