# Live-Code Quickstart

Let's get started!  Below is a live demo REPL that is ready to run five simple demos.  Go ahead and click the Green run button.

{% embed url="<https://replit.com/@EricEverman/AndHowHelloWorld?lite=true>" %}

{% tabs %}
{% tab title="Overview" %}
*If you haven't already, go ahead and click the green run button* <img src="https://2281660175-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MkItIobWMJCrFRNX6Iw%2Fuploads%2FDb1LhgbSfW2z0rKyuq8e%2Fgreen%20run%20button.png?alt=media&#x26;token=97649567-2d0b-406e-8409-44629267dfed" alt="" data-size="line"> *at the top of the* REPL *window above.*

Running this REPL compiles a small AndHow example, then runs it five times with different configurations.  Take a look at the `Main` class:  It contains two AndHow Properties, `NAME` & `REPEAT_COUNT`. The values of these two Properties are used to print to System.out in the main method.

There is also an ***andhow\.properties*** file - click the file icon <img src="https://2281660175-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MkItIobWMJCrFRNX6Iw%2Fuploads%2FVOgMgdxoTjXiWLcLCYiX%2FScreen%20Shot%202021-10-23%20at%209.12.14%20PM.png?alt=media&#x26;token=1ac11a9b-6d91-45e9-bf49-346cc5b73503" alt="" data-size="line"> on the left to see the list of files.  The file has a value for both Properties.  (You can click the file icon again to make the list go away).

**Continue on to the Run 1 tab. . .**

{% hint style="info" %}
This REPL is hosted by Repl.it.  You can work on the Console or the Shell without logging in, but you need to login with a free accont to work on a fork to modify code or files.

If you try to run commands on the console and get an error like this:

`Error: Could not find or load main class Main Caused by: java.lang.ClassNotFoundException: Main`

Your VM has timed out - Just click the green run button again to rebuild everything.
{% endhint %}
{% endtab %}

{% tab title="Run 1" %}
*You probably have to scroll back up to the top of the black **Console** window, above, to see the output of Run 1.*

#### Run 1 prints:  <mark style="color:blue;">Hello, Dawn! Hello, Dawn!</mark>

AndHow found and loaded the values for the two Properties from the ***andhow\.properties*** file as soon as the code tried to access a Property value (`Main` line 13).  By default, AndHow will always attempt to read property values from that file.

The command in yellow:

<mark style="color:yellow;">**java -cp .:target/dependency/\*:target/classes Main**</mark>

...is the command that ran this example.  You can copy and paste it in the console and run it again.  Clicking the green run button will compile the code and run all four examples again - you don't need to click the run button again unless you want to see the whole thing run again.

**Continue on to the Run 2 tab, and so on. . .**
{% endtab %}

{% tab title="Run 2" %}

#### Run 2 prints:  <mark style="color:blue;">Hello, Darcey! Hello, Darcey!</mark>

In this example, an environment variable named *Main.NAME* is set to *Darcey* and the `java` command is run with that variable set.

AndHow scans the environment variable names for any that match a Property and assigns *Darcey* to the `NAME` Property.  For AndHow, env. vars. take precidence over values from property files, so *Darcey* is used instead of *Dawn.*  The REPEAT\_COUNT value of 2 is still taken from the properties file.

AndHow has a [well defined priority order of configuration sources](https://www.andhowconfig.org/user-guide/loaders-and-load-order) that works for most situations and the ability to [change the order](https://www.andhowconfig.org/user-guide/changing-the-load-order) if needed.
{% endtab %}

{% tab title="Run 3" %}

#### Run 3 prints:  <mark style="color:blue;">Hello, Dave! Hello, Dave!</mark>

This time NAME is set as a java system property via the `-D` argument.  AndHow can read configuration from many different sources.
{% endtab %}

{% tab title="Run 4" %}

#### Run 4 prints:  <mark style="color:blue;">Hello, Demi! Hello, Demi!</mark>

The java command arguments after the class name are passed to the main(String\[] args) method.  The main method includes this bit of code:

```java
AndHow.findConfig().setCmdLineArgs(args);
```

AndHow can load values from most configuration sources automatically, however, it has no way to intercept the command line arguments passed to the main method - the application has to help by passing them to AndHow.  Read more about how [findConfig()](https://www.andhowconfig.org/user-guide/configuring-andhow#the-andhow.findconfig-method) works.
{% endtab %}

{% tab title="Run 5" %}

#### Run 5 uses a system property to set NAME to an invalid value

`NAME` in the `Main` class is built with **`startsWith("D")`** - It must start with D as all the examples have done.  What happens if that rules is broken?

This example was saved for you to run.  Copy and paste the yellow comand in the console window:

<mark style="color:yellow;">**java -cp .:target/dependency/\*:target/classes -DMain.NAME=Bob Main**</mark>

...Did you run it?

AndHow validates all configuration values at startup and throws a `RuntimeExcpetion` to [fail fast](https://www.andhowconfig.org/user-guide/key-concepts#andhow-fails-fast...-and-that-is-a-good-thing) and prevent an application from running with invalid configuration.  The validation errors and messages AndHow gives are very specific:

`Property Main.NAME loaded from java.lang.System.getProperties(): The value 'Bob' must start with 'D'`

It tells us where the value was loaded from and exactly what validation rule the potential configuration value broke.  It also includes a message like this:

<mark style="color:red;">`A set of sample configuration files will be written to '/tmp/andhow-samples/'`</mark>

When a startup error happens, AndHow helpfully creates *configuration templates*.  These templates are rich and detailed files that serve as documentation for application configuration and a starting point for creating a properties configuration file.  You can list and view the contents in the console:

```bash
ls /tmp/andhow-samples/     (your path may be different)
  - JNDI.xml
  - PropertyFile_KeyValuePair.properties
more /tmp/andhow-samples/PropertyFile_KeyValuePair.properties
```

Here is a portion from the PropertyFile\_KeyValuePair.properties file - a template file you can use to create your own andhow\.properties file:

```properties
# 
# NAME (String) NON-NULL
# The property value must start with 'D'
Main.NAME = 

# 
# REPEAT_COUNT (Integer) NON-NULL
# The property value must be less than 5
Main.REPEAT_COUNT = 
```

{% endtab %}
{% endtabs %}

### Where to go next

The [User Guide](https://www.andhowconfig.org/user-guide) has a suggested learning path - Happy trails!

***&?!***
