Setting up the Run File Command


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.

If you are using a post-5.0 version of NetBeans IDE, try to run the Run File command before writing a target. The IDE might offer to generate the target for you, which could save you some time.


Creating the run-selected-file Target

Following 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

  1. In the Files window, expand the project's folder, expand the nbproject folder, and then open the project.xml file.

  2. Within the <ide-actions> element, add a mapping for the Run File command. The mapping might look something like the following example:

 <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.



NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net