#2886 Importing other pods in Java FFI

SlimerDude Sat 4 Feb 2023

Hi, if I have a Java FFI Peer class that defines native methods, how would I configure the build.fan, or the Fantom build environment in general, to allow the class to use other pods?

For example, the following Java FFI class and build.fan:

package fan.acme;

import fan.sys.*;

// not sure how to compile these lines??
import fan.web.WebOutStream;
import fan.web.Cookie;

// or these!?
//import fan.graphics.Size;
//import fan.graphics.Point;

public class SomePeer {
    ...
}
class Build : build::BuildPod {

  new make() {
    podName = "acme"
    summary = "acme"

    depends = [
      "sys    1.0.78 - 1.0",
      "web    1.0.78 - 1.0",
    ]

    srcDirs  = [`fan/`]
    javaDirs = [`java/`]
  }
}

When built, gives the following error:

C:\acme> fan build.fan
compile [acme]
  Compile [acme]
    FindSourceFiles [2 files]
    CompileJs
    WritePod [file:/C:/Apps/fantom-1.0.78/lib/fan/acme.pod]
  javaNative [afDom]
    Exec [C:\Apps\Java\openjdk-1.8.0.161\bin\java.exe 
          -cp C:\Apps\fantom-1.0.78\lib\java\sys.jar 
          -Dfan.home=C:\Apps\fantom-1.0.78
          fanx.tools.Jstub -d C:\acme\temp-java acme]
    Java Stub [acme]
    CompileJava

C:\acme\java\SomePeer.java:5: error: package fan.web does not exist
import fan.web.WebOutStream;
              ^
C:\acme\java\DocPeer.java:6: error: package fan.web does not exist
import fan.web.Cookie;
              ^
2 errors
ERR: CompileJava failed
        sys::Err
BUILD FAILED [881ms]!

I'm confident I can find a work around for this (Java class path?) issue, but if I'm just missing a line of config, I'd much rather setup, implement, or configure the official Fantom easy way!

brian Sat 4 Feb 2023

The build task will automatically put all your dependent pods into the javac classpath. However, all of your depend pods must have their code pre-generated as Java class files for javac to use. The web pod will not since it has no native code. However, in that case you can use the jstub tool to force your dependent pods to be compiled to Java ahead of time.

SlimerDude Sat 4 Feb 2023

Thanks Brian, I had a feeling that would be the case - but wanted to make sure!

Login or Signup to reply.