如何在Eclipse运行javac如何在、Eclipse、javac

2023-09-08 09:18:10 作者:习惯了一个人面对所有

我想运行一个编译后的.class文件在Eclipse的javac工具。我打开外部工具配置他们填写filds:

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

位置: C:\ Program Files文件\的Java \ jdk1.6.0_25 \斌\器javac.exe

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

工作目录: $ {workspace_loc:/主/ bin中}

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

参数:

我要问你什么,我必须写在参数字段,而我是填*的位置的*和工作目录:场对吧?

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

推荐答案

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

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.

设置一个外部工具配置的javac

下面是需要设置javac编译器,因此它可以在Eclipse中使用的步骤(可以使用启动配置如下图):

Here are the steps required to setup the javac compiler so it can be used inside Eclipse (ready to use launch configurations are below):

创建一个新的External工具配置。 设置配置的位置给javac可执行文件的路径(如 C:\ Program Files文件(x86)的\的Java \ jdk1.7.0_25 \斌\器javac.exe )。 设置的javac到项目的类路径中的-classpath选项配置的参数字段中(例如: -classpath $ {project_classpath} )。 设置的javac的-d选项的项目(例如 -d $ {project_loc} \ BIN )的二进制文件夹中。 添加任何额外的javac选项像-verbose 或α参数参数列表。 添加路径到源文件或源文件夹中的结束参数列表的(例如: $ {selected_resource_loc} 选中源文件或 $ {selected_resource_loc} \ * 为选择的包)。完整的参数字段的配置可能看起来像这样: -verbose -classpath $ {project_classpath} -d $ {project_loc} \ BIN $ {selected_resource_loc} \ * Run外部工具配置中选定的文件上。 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 options like "-verbose" or "-parameters" to the argument list. 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}\* 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.

我创建了两个默认的启动配置的javac,你可以通过把它们与项目文件夹.launch(如javac.launch)结尾的文件重用。一旦你打开外部工具配置对话框Eclipse将自动检测到这些配置文件。你很可能需要改变的javac的位置的javac的位置,您的计算机上。

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 - 对单个选定文件中的-verbose选项启动javac的:

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>

文件的javac(DIR).launch - 选定的包装上启动javac的:

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>

添加的javac到Eclipse构建链项目

添加的javac到构建链,因此它就会自动在完整或自动构建以类似的方式完成如上所述执行:

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的启动配置(见上文),可以通过导入它作为一个新的建设者。否则,你可以创建一个新建...建设者javac的。 您将最有可能要更改参数的javac的满足您的需求。首先,你应该改变-d参数来建项目的二进制文件夹: -d $ {build_project} \ BIN 。然后,你应该添加源文件/文件要通过的javac编译使用$ {resource_loc}变量参数列表的末尾文件夹。完整的参数列表编译一个源文件可能看起来像这样: -classpath $ {project_classpath} -d $ {build_project} \ BIN $ {resource_loc:MyProject的/ src目录/ mypackage中/ MyClass.java} 。要编制一个完整的软件包,你可以写 $ {resource_loc:MyProject的/ src目录/ mypackage中} \ * 而不是 选择构建选项选项卡配置,当javac的建设者应该运行。你可能会想取消干净后。 如果你只是想添加的javac作为aditional的编译器的JDT编译一些源文件的顶部,然后你做。如果你完全要替换的JDT编译器,您必须从建设者选项卡中取消选择的Java生成器,将可能要增加一个新的工具来构建链,执行内置的清理操作(否则类文件将不会被内置)。中删除 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).