#1235 Call fantom from java/c#

roja Mon 27 Sep 2010

Hi all,

I was wondering if anyone could point me to the syntax for calling fantom code from java or c#?

ivan Mon 27 Sep 2010

Hi roja,

There's no any special syntax. Fantom classes are converted to Java classes in runtime. So Fantom class myPod::MyClass is being converted to fan.myPod.MyClass java class and you can use it normally from your Java application.

However, to make sure the class you need is already emitted, you either need to correctly work with Environment, or (which is more simple to start with) use JarDistEnv.

Here's an example of how it works: At first you create a normal Fantom pod, which has directory structure like this:

./myPod/
  fan/
    MyClass.fan
  build.fan

For example, MyClass.fan looks like this:

class MyClass
{
  Str padl(Str str, Int len, Int char := ' ') 
  {
    str.padl(len, char)
  }

  static Void main(Str[] args)  {}
}

At second, create a buildJar.fan and place it near your build.fan. Here's the contents of buildJar.fan:

class Build : BuildScript
{
  @Target { help = "build myPod pod as a single JAR dist" }
  Void distFansh()
  {
    dist := JarDist(this)
    dist.outFile = `./myPod.jar`.toFile.normalize
    dist.podNames = Str["myPod"]
    dist.mainMethod = "myPod::MyClass.main"
    dist.run
  }
}

Then, run both scripts from console:

komaz-mac:myPod ivaninozemtsev$ fan build.fan
compile [myPod]
  Compile [myPod]
    FindSourceFiles [1 files]
    WritePod [file:/Users/ivaninozemtsev/work/dev/fan/lib/fan/myPod.pod]
BUILD SUCCESS [171ms]!

komaz-mac:fanSample ivaninozemtsev$ fan buildJar.fan 
JarDist
  Pod [sys]
  Pod [myPod]
    JStub to classfiles
  Exec [/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/bin/java -cp /Users/ivaninozemtsev/work/dev/fan/lib/java/sys.jar -Dfan.home=/Users/ivaninozemtsev/work/dev/fan fanx.tools.Jstub -d /Users/ivaninozemtsev/work/dev/fan/temp/jardist-899a39c95cdd7500 myPod]
    Java Stub [myPod]
  Main
  Exec [/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/bin/javac -cp /Users/ivaninozemtsev/work/dev/fan/temp/jardist-899a39c95cdd7500 -d /Users/ivaninozemtsev/work/dev/fan/temp/jardist-899a39c95cdd7500 /Users/ivaninozemtsev/work/dev/fan/temp/jardist-899a39c95cdd7500/fanjardist/Main.java]
  Manifest
  Jar [file:/Users/ivaninozemtsev/work/bitbucket/workspace/myPod/myPod.jar]
  Exec [/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/bin/jar cfm /Users/ivaninozemtsev/work/bitbucket/workspace/fanSample/fanSample.jar /Users/ivaninozemtsev/work/dev/fan/Manifest.mf -C /Users/ivaninozemtsev/work/dev/fan/temp/jardist-899a39c95cdd7500 .]
  Success [file:/Users/ivaninozemtsev/work/bitbucket/workspace/myPod/myPod.jar]
BUILD SUCCESS [4982ms]!
> 

As a result, you'll get a myPod.jar file which you can use as any normal Java library from your project, for example like this:

package org.fantom.javaSample;

import fan.myPod.MyClass;

public class Program {
  public static void main(String[] args) {
    MyClass a = new MyClass();
    System.out.println(a.padl("a", 10, '_'));
  }
}

Result is:

_________a

katox Mon 27 Sep 2010

Very nice wrap-up. This could be added to docs as is.

roja Mon 27 Sep 2010

Excellent,

  • Am I OK to assume that the mechanism is similar when it comes to C#?
  • You mention that Java class files are emitted at runtime, is there any way to push that step to build time?

ivan Mon 27 Sep 2010

JarDistEnv emits all classes at compile time, so the result jar file contains all necessary class files.

Regarding .NET can't give any recipes, never tried it :(. I assume that you'll have to have fan/lib/dotnet/sys.dll and all necessary pod files somewhere on your PATH (probably with configured FAN_HOME environment variable) and use Fan.Sys.Type.find("myPod::MyClass") to load Fantom type (and then probably instanciate objects with reflection api).

Probably you can analyze the source of build::JarDist and create DotnetDist which will emit all necessary classes and make a .NET assembly from pods.

go4 Mon 27 Sep 2010

I don't know What are jstub and nstub used to do. Is there something to do with this?

andy Mon 27 Sep 2010

We don't yet have the equivalent of JarDistEnv for .NET.

Login or Signup to reply.