如何从 Eclipse 运行 Javac
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在某些情况下,从 Eclipse 中启动 javac 编译器可能是一个非常有用的功能(例如,出于测试目的、将 javac 输出与 Eclipse 编译器的输出进行比较、使用特殊 javac 编译器选项或使用以下编译器重新编译各个类文件)不同的 JDK 版本等)。除了使用 ant,有两种方便的方法将 javac 集成到 Eclipse 中:为 javac 设置“外部工具配置”,或者将 javac 添加到项目的 Eclipse 构建链中。
为 javac 设置“外部工具配置”
以下是设置 javac 编译器所需的步骤,以便可以在 Eclipse 中使用它(准备使用的启动配置如下):
C:\Program Files (x86)\Java\jdk1.7.0_25\bin\javac.exe
)。-classpath ${project_classpath}
)。-d ${project_loc}\bin
)。${selected_resource_loc}
表示所选源文件或${selected_resource_loc} \*
表示所选包)。配置的完整“参数”字段可能如下所示:-verbose -classpath ${project_classpath} -d ${project_loc}\bin ${selected_resource_loc}\*
除此之外,您可能需要在工具配置的“刷新”选项卡下为所选项目选择“完成后刷新资源”,并可能在“构建”选项卡下取消选择“启动前构建”。
我为 javac 创建了两个默认启动配置,您可以通过将它们放入项目文件夹中以“.launch”结尾的文件(例如“javac.launch”)中来重用它们。一旦您打开“外部工具配置”对话框,Eclipse 将自动检测这些配置文件。您很可能需要将 javac 的位置更改为计算机上 javac 的位置。
文件“javac(详细文件).launch” - 在单个选定文件上使用 -verbose 选项启动 javac:
文件“javac (dir).launch” - 启动 javac在选定的包上:
将 javac 添加到项目的 Eclipse 构建链
将 javac 添加到构建链,以便在完整或自动构建期间自动执行它,其方式与上述类似:
-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}\*
。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):
C:\Program Files (x86)\Java\jdk1.7.0_25\bin\javac.exe
).-classpath ${project_classpath}
).-d ${project_loc}\bin
).${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}\*
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:
File "javac (dir).launch" - launches javac on the selected package:
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:
-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.创建Java应用程序启动配置
在运行配置里面---> Main选项卡,您可以在Run Configuration里面指定要运行的Main类
--->参数选项卡,您可以指定传递到主类中的参数。只需在此处输入您的输入参数,每个参数均以空格分隔(例如 arg1 arg2 arg3 会将这 3 个参数传递给您的主类)。您还可以使用 Eclipse 预定义变量
folder_prompt
、file_prompt
或string_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 variablesfolder_prompt
,file_prompt
orstring_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 argumentsInside the Run Configuration ---> JRE tab ,you can specify the JRE to execute the main class.
使用蚂蚁。
创建使用 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