Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Javac from Eclipse

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 ?

like image 891
Viktor Apoyan Avatar asked May 13 '11 05:05

Viktor Apoyan


1 Answers

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).
like image 112
Balder Avatar answered Sep 23 '22 14:09

Balder