[[cloudBalancingMainMethod]]
= Run the cloud balancing Hello World
:imagesdir: ../../..

. <<runTheExamplesInAnIDE,Download and configure the examples in your preferred IDE.>>
. Create a run configuration with the following main class: `org.optaplanner.examples.cloudbalancing.app.CloudBalancingHelloWorld`
+
By default, the Cloud Balancing Hello World is configured to run for 120 seconds.


It executes the following code:

.CloudBalancingHelloWorld.java
====
[source,java,options="nowrap"]
----
public class CloudBalancingHelloWorld {

    public static void main(String[] args) {
        // Build the Solver
        SolverFactory<CloudBalance> solverFactory = SolverFactory.createFromXmlResource(
                "org/optaplanner/examples/cloudbalancing/solver/cloudBalancingSolverConfig.xml");
        Solver<CloudBalance> solver = solverFactory.buildSolver();

        // Load a problem with 400 computers and 1200 processes
        CloudBalance unsolvedCloudBalance = new CloudBalancingGenerator().createCloudBalance(400, 1200);

        // Solve the problem
        CloudBalance solvedCloudBalance = solver.solve(unsolvedCloudBalance);

        // Display the result
        System.out.println("\nSolved cloudBalance with 400 computers and 1200 processes:\n"
                + toDisplayString(solvedCloudBalance));
    }

    ...
}
----
====

The code example does the following:

. Build the `Solver` based on a solver configuration
which can come from <<solverConfigurationByXML,an XML file>> as classpath resource:
+
[source,java,options="nowrap"]
----
        SolverFactory<CloudBalance> solverFactory = SolverFactory.createFromXmlResource(
                "org/optaplanner/examples/cloudbalancing/solver/cloudBalancingSolverConfig.xml");
        Solver<CloudBalance> solver = solverFactory.buildSolver();
----
+
Or to avoid XML, build it through <<solverConfigurationByJavaAPI,the programmatic API>> instead:
+
[source,java,options="nowrap"]
----
        SolverFactory<CloudBalance> solverFactory = SolverFactory.create(new SolverConfig()
                .withSolutionClass(CloudBalance.class)
                .withEntityClasses(CloudProcess.class)
                .withEasyScoreCalculatorClass(CloudBalancingEasyScoreCalculator.class)
                .withTerminationSpentLimit(Duration.ofMinutes(2)));
        Solver<CloudBalance> solver = solverFactory.buildSolver();
----
+
The solver configuration is explained in <<cloudBalancingSolverConfiguration, the next section>>.

. Load the problem.
+
`CloudBalancingGenerator` generates a random problem: replace this with a class that loads a real problem, for example from a database.
+
[source,java,options="nowrap"]
----
        CloudBalance unsolvedCloudBalance = new CloudBalancingGenerator().createCloudBalance(400, 1200);
----

. Solve the problem.
+
[source,java,options="nowrap"]
----
        CloudBalance solvedCloudBalance = solver.solve(unsolvedCloudBalance);
----

. Display the result.
+
[source,java,options="nowrap"]
----
        System.out.println("\nSolved cloudBalance with 400 computers and 1200 processes:\n"
                + toDisplayString(solvedCloudBalance));
----
