
HelloWorld
Setup
Fan is distributed as a simple zip file you will need to unzip to your local machine. Make sure the correct binary directory is included in your path:
{fan.home}/bin/win // if running on Windows {fan.home}/bin/unix // if running on Unix/Mac TODO
When you first install Fan it will automatically try to run using your currently configured Java VM (requires Java 1.5 or greater). If things are working correctly you should be able to run "fan -version":
C:\dev\fan\bin\win>fan -version Fan Launcher Copyright (c) 2006-2008, Brian Frank and Andy Frank Licensed under the Academic Free License version 3.0 Java Runtime: java.vm.name: Java HotSpot(TM) Client VM java.vm.vendor: Sun Microsystems Inc. java.vm.version: 1.6.0_03-b05 java.home: C:\Program Files\Java\jre1.6.0_03 fan.home: c:\dev\fan sys.version: 1.0.19
If that doesn't work then try these options:
- Install Java 1.5 or greater and retry
- Explicitly configure where your JVM is installed
- Switch to use the .NET runtime
- Turn on launcher debugging
Fan Shell
The Fan shell is a command line tool for evaluating expressions and statements. It is a great way to test things out. To launch the shell run the fansh
executable and call the Obj.echo
method:
C:\dev\fan\bin\win>fansh Fan Shell v1.0.19 ('?' for help) fan> echo("hello world #1") hello world #1 fan> quit
Checkout docTools::Fansh for more details on the fan shell.
Fan Script
You can also execute any file with the ".fan" extension as a script file. The script must contain a full class definition with a method called "main". Create a file called "hello.fan":
class Hello { static Void main() { echo("hello world #2") } }
Pass the script file name to the fan
executable:
C:\dev\fan\bin\win>fan hello.fan hello world #2
Note that unlike Java or C# the arguments aren't passed as parameters to main - rather you can access them via Sys.args
.
Checkout docTools::Fan for more details running fan scripts. Also see setup to make fan scripts executable without calling the launcher explicitly.
Fan Pod
For production systems, you typically organize your code into precompiled modules called pods. Pods are built using Fan's build toolkit. To build a new pod called "hello" create a directory structure organized as follows:
hello/ build.fan fan/ Main.fan
The contents of "build.fan" is the build script. It declares the meta-data for your pod and which directories contain Fan source files:
using build class Build : BuildPod { override Void setup() { podName = "hello" version = Version("1.0") description = "hello world example" depends = ["sys 1.0"] srcDirs = [`fan/`] } }
The "fan/Main.fan" file declares a single class called "Main":
class Main { static Void main() { echo("hello world #3") } }
The build file itself is just a normal Fan script file which will compile the pod:
C:\dev\fan\src\hello>fan build.fan compile [hello] Compile [hello] FindSourceFiles [1 files] WritePod [/C:/dev/fan/lib/fan/hello.pod] BUILD SUCCESS [70ms]!
If you look in your "lib/fan" directory you should now see a file called "hello.pod". Assuming you called your method "main" in a class called "Main" you can run the main method using the fan
executable:
C:\dev\fan>fan hello hello world #3 C:\dev\fan>fan hello::Main hello world #3 C:\dev\fan>fan hello::Main.main hello world #3
Checkout docTools::Fan for more details running methods in a pod, and docTools::Build for details on the build toolkit.