如何从 Eclipse 运行 Javac

发布于 2024-11-07 07:09:03 字数 362 浏览 2 评论 0原文

我正在尝试在 Eclipse 中编译的 .class 文件上运行“javac”工具。我打开外部工具配置,填充字段:

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

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

参数: ?

我想问一下,我必须在 Arguments 字段中填写什么内容,我是否填写了*Location* 和 工作目录: 字段?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尤怨 2024-11-14 07:09:03

在某些情况下,从 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. 将 javac 的“-classpath”选项设置为配置的“Arguments”字段内项目的类路径(例如 -classpath ${project_classpath})。
  4. 将javac 的“-d”选项设置为项目的二进制文件夹(例如-d ${project_loc}\bin)。
  5. 添加任何其他 javac 选项,例如“ -verbose”或“-parameters”到参数列表。
  6. 将源文件或源文件夹的路径添加到参数列表的末尾(例如,${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(详细文件).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>

文件“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>

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

将 javac 添加到构建链,以便在完整或自动构建期间自动执行它,其方式与上述类似:

  1. 打开项目属性并选择“构建者”选项卡。然后,如果您已经有 javac 的启动配置(见上文),您可以将其“导入...”作为新的构建器。否则,您可以为 javac 创建一个“New...”构建器。
  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 编译器,则必须从“Builders”选项卡中取消选择“Java Builder”,并且可能需要向构建链添加一个新工具,该工具执行构建的清理操作(否则类文件在构建过程中不会被删除)。

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).
始于初秋 2024-11-14 07:09:03

创建Java应用程序启动配置

在运行配置里面---> Main选项卡,您可以在Run Configuration里面指定要运行的Main类

--->参数选项卡,您可以指定传递到主类中的参数。只需在此处输入您的输入参数,每个参数均以空格分隔(例如 arg1 arg2 arg3 会将这 3 个参数传递给您的主类)。您还可以使用 Eclipse 预定义变量 folder_promptfile_promptstring_prompt 对参数进行参数化(您可以使用内部的“variables...”按钮参数选项卡可帮助您配置它们)。当您运行主类时,Eclipse 将提示对话框让您

在运行配置中输入参数 ---> JRE选项卡,可以指定执行主类的JRE。

Creating a Java application launch configuration

Inside the Run Configuration ---> Main tab , you can specify the Main class to run

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

Inside the Run Configuration ---> JRE tab ,you can specify the JRE to execute the main class.

末骤雨初歇 2024-11-14 07:09:03

使用蚂蚁。

创建使用 javac 任务的 Ant 构建文件并使用 eclipse Ant 视图运行它。

Eclipse Ant文档

Ant 手册:Javac 任务

Use Ant.

Create an Ant buildfile that uses the javac task and run it using the eclipse Ant view.

Eclipse Ant Documentation

Ant Manual: Javac task

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文