Problem

You want to develop and run your application in Eclipse.

Solution

Download the latest version of Eclipse here.

Create a new project. On the Java Settings page, go to the "Libraries" tab and add the agi.parallel.client-[version].jar to the project. It can also be added later by using Project > Properties > Java Build Path > Libraries > Add External JARS

Configure Eclipse Step 1

Optionally configure Javadoc. On the "Libraries" tab, expand agi.parallel.client-1.1.0.jar and double click on the Javadoc location.

Configure Eclipse Step 2

In the Javadoc for 'agi.parallel.client-1.1.0.jar' panel, select the "Javadoc in archive" option and browse to the location of agi.parallel.javadoc-1.1.0.jar. This is located at [PathToSTKScalabilityApiJavaFolder]\Documentation\agi.parallel.javadoc-1.1.0.jar

Copy and paste EclipseWithStkScalability.java to the project.

Java  Copy imageCopy
import agi.parallel.client.ClusterJobScheduler;
import agi.parallel.client.IJobScheduler;
import agi.parallel.client.Job;
import agi.parallel.infrastructure.Task;

public class EclipseWithStkScalability {
    public static void main(String[] args) {
        IJobScheduler scheduler = new ClusterJobScheduler("localhost");
        try {
            scheduler.connect();
            Job job = scheduler.createJob();
            Task task = new MyTask();
            job.addTask(task);
            job.submit();
            job.waitUntilDone();
            System.out.println("Result = " + task.getResult());
        } finally {
            scheduler.dispose();
        }
    }

    public static class MyTask extends Task {
        @Override
        public void execute() {
            System.out.println("Line 1");
            System.out.println("Line 2");
            this.setResult("OK");
        }
    }
}

Next, compile the project as a jar and set up Eclipse to run the jar instead of the class files directly.

Go to File > Export > Java > JAR file

Configure Eclipse Step 3

Save the jar. Take note of the location of the JAR file that you specified.

Configure Eclipse Step 4
Note

To run the application in Eclipse, the jar file needs to be rebuilt every time the source files are updated. For instructions to build the jar automatically, see details in the Discussion section.

The last step is to make sure that the Run configuration is configured to run the created jar file.

Go to Run > Run Configurations > Java Application. In the "Classpath" tab, click on "Add External Jars" and select the created jar above. Make sure to move the jar to the first entry in "User Entries."

Configure Eclipse Step 5

Finally, run the application.

Discussion

One of the most common questions from developers relates to IDEs and tasks. Although the documentation gives instructions on how to compile applications using the command line, there is no reason IDEs can't be used to develop and debug tasks. The examples included in the developer kit also include Eclipse project files and ant scripts. To run the examples from Eclipse, import the example's project file into the Eclipse workspace.

Eclipse has the capability to automatically rebuild the jar file before every run to avoid the manual process of exporting the jar file each time a source file changes. To autobuild the jar file, run an ant builder that packages the project into a jar after each compilation. In Eclipse, right click on the project and click "Properties". On the properties page, go to the "Builders" tab, and add the ant buildfile. An ant script that can build the jar file will be needed. Included below is an example ant script that can be used as a template.

  Copy imageCopy
<project name="EclipseWithStkScalability" basedir="." default="package">
    <property name="jar.name" value="EclipseWithStkScalability.jar" />

    <property name="output.dir" value="ant" />
    <property name="bin.dir" value="${output.dir}/bin" />
    <property name="dist.dir" value="dist" />

    <target name="clean" description="Clean all generated output.">
        <delete dir="${output.dir}" />
        <delete dir="${dist.dir}" />
    </target>

    <target name="prepare">
        <mkdir dir="${bin.dir}" />
        <mkdir dir="${dist.dir}" />
        <path id="project.classpath">
            <fileset dir="lib" includes="*.jar" />
        </path>
    </target>

    <target name="compile" depends="prepare" description="Compile source code.">
        <javac source="1.5" target="1.5" destdir="${bin.dir}" classpathref="project.classpath" includeantruntime="no">
            <src path="src" />
        </javac>
    </target>

    <target name="package" depends="compile" description="Package the application into a jar, and copy all needed resources to the dist directory.">
        <!-- Copy library jars to the dist directory -->
        <copy todir="${dist.dir}" flatten="true">
            <path refid="project.classpath" />
        </copy>

        <!-- Build the jar file containing the application itself -->
        <copy todir="${bin.dir}">
            <fileset dir="src" excludes="**/*.java" />
        </copy>

        <pathconvert property="manifest.classpath" pathsep=" ">
            <path refid="project.classpath" />
            <flattenmapper />
        </pathconvert>

        <jar destfile="${dist.dir}/${jar.name}">
            <fileset dir="${bin.dir}" />
            <manifest>
                <attribute name="Class-Path" value="${manifest.classpath}" />
                <attribute name="Main-Class" value="EclipseWithStkScalability" />
            </manifest>
        </jar>
    </target>

    <target name="run" depends="package" description="Build and run the demo application.">
        <java fork="true" spawn="true" jar="${dist.dir}/${jar.name}" dir="${dist.dir}" />
    </target>
</project>
Tip

Note that the project.classpath property in this ant script is set to the lib directory of the project folder. You will have to copy agi.parallel.client-1.1.0.jar to the lib folder in order to use the ant file to compile and run.

Before closing the "Edit Configuration" panel, go to the "Targets" tab. On the "Manual Build" and "Auto Build" options, select the ant target that packages the project into a jar file. For the ant script above, this is the package target.

Configure ant autobuild

If done correctly, this configures Eclipse to build the jar automatically via ant during every compilation.

Tip

If an error is given that the built jar cannot be found when trying to execute your program, try refreshing the project/package explorer view in Eclipse.

See Also

Other Resources