golo-lang icon indicating copy to clipboard operation
golo-lang copied to clipboard

How to build and use a jar library with Golo for Java?

Open k33g opened this issue 9 years ago • 5 comments

If I compile golo code with this:

golo compile --output hello.jar samples/*.golo

Can I use the jar file from java? How can I call module function from Java code?

k33g avatar Aug 21 '16 08:08 k33g

Hi @k33g ,

first, you have to add Golo runtime dependencies in your classpath:

$ ls lib/
asm-5.1.jar  
golo-3.2.0-SNAPSHOT.jar  
jcommander-1.55.jar  
json-simple-1.1.1.jar  
txtmark-0.13.jar

then you can run any Golo sample like this:

# Fibonacci for example
$ java -cp hello.jar:lib/* samples.Fibonacci
>>> 102334155 (took 1610ms)
>>> 102334155 (took 1545ms)
>>> 102334155 (took 1546ms)
>>> 102334155 (took 1542ms)
^C

Golo functions are just Java static methods.

So let's call Golo from Java:

import samples.Fibonacci;

public class FromJava {

  public static void main(String[] args){
   System.out.println(Fibonacci.fib(42));
  }

}
$ javac -cp lib/golo-3.2.0-SNAPSHOT.jar:hello.jar FromJava.java
$ java -cp hello.jar:lib/*:. FromJava
267914296
$

Many things can be done from Java (structs, dynamic objects, ...) but there's some things you can't do from java, for exemple augmentations are only resolved from Golo code :

import samples.Augmentations;
import java.util.*;

public class FromJava {

  public static void main(String[] args){

    //List l = new LinkedList();
    //l.with(foo); // this is not possible from Java

    Augmentations.main(args); // but this works as expected

  }

}
$ javac -cp lib/golo-3.2.0-SNAPSHOT.jar:hello.jar FromJava.java
$ java -cp hello.jar:lib/*:. FromJava
>>> foo
>>> bar
>>> baz
$

I hope it will help

artpej avatar Aug 21 '16 09:08 artpej

@Artpej thx a lot (I failed because of naming conventions), now thanks to you it's perfect 👍

k33g avatar Aug 21 '16 09:08 k33g

@jponge perhaps this is something to add to the documentation or @yloiseau to «Real World Golo» before closing this issue

k33g avatar Aug 21 '16 09:08 k33g

I had in mind to implement the JSR 223 support for Golo. This could ease the Java integration but it's not as simple as I expected.

Any thoughts about this?

artpej avatar Aug 21 '16 09:08 artpej

@Artpej That'd be lovely

jponge avatar Aug 22 '16 20:08 jponge