To be able to run a selected file in a free-form project in the IDE, you need to create an Ant target for the command and then map that target in the project's project.xml file.
Creating the run-selected-file TargetFollowing is a sample target for running selected files: <target name="run-selected-file" depends="compile-selected-files-in-src" description="Run Single File"> <fail unless="selected-file">Must set property 'selected-file'</fail> <java classname="${selected-file}"> <classpath ref/> </java> </target> In this example, the selected-file property picks up the file that you have selected in the IDE. The value of selected-file is passed from the project.xml file when you choose the Run File command. This example also assumes that you have a working compile-selected-files-in-src target (like the example in Setting up the Compile File Command earlier in this chapter), although it is also possible to have the target depend on a different compile target you have set in your script. The example uses the refid attribute to reference a run classpath that must be defined elsewhere in the script, with run.classpath specified as the id attribute of a path element. For example, run.classpath could be defined as in the following snippet: <path > <pathelement path="${javac.classpath}"> <pathelement path="${build.classes.dir}"> </path> The paths for javac.classpath and build.classes.dir would need to be defined in their own path elements. If the path element needs to reference a physical location, the location attribute could be used to specify a directory relative to the Ant project's base directory. For example: <path > <pathelement location="classes"> </path> Mapping the run-selected-file Target to the IDE Command
<action name="run.single"> <target>run-selected-file</target> <context> <property>selected-file</property> <folder>${src.dir}</folder> <pattern>\.java$</pattern> <format>java-name</format> <arity> <one-file-only/> </arity> </context> </action> See Table 16-6 for a description of the parts of the <action> element in the project.xml file. See Handling Properties in the project.xml File earlier in this chapter for information on calling properties from the project.xml file. |