#2729 Native Java in Pod

jhughes Tue 29 Jan 2019

I have been trying to understand the use cases of native code and the option I believe that works best for what i'm trying to do is include a a few java source files, not an entire jar, into a pod. I first attempted to just translate the majority of these java classes into fantom but there are too many interop calls that just don't seem to be ending up with the same result as the native java code. I then began reading and it seemed like I should be able to add these java files directly into a directory of my project and reference it with the javaDirs path in the build script but I F4 just keeps telling me

Errors occurred during the build. Errors running builder Script Builder on project nativeTestProject. sys::Err: org.eclipse.core.runtime.CoreException: The file does not exist for the external tool named Javac configutation

And the f4 error console tells me:

The container Fantom Native Libraries (Java) references non existing library C:\Fantom\fantom-1.0.69\bin\nativeTestProject.pod

So am i going about this in a way that's not supported or is there a way to include your java native code in the same build script of the fan code and get them to link together? These errors are pretty cryptic to me right now and I have no idea if this is some configuration that needs to get fixed in fantom, F4, my system or if i'm just going about this in a way that's unsupported.

brian Tue 29 Jan 2019

I would suggest working from the command line using the Fantom tools. Then from there move to use F4. That will let you narrow down if its just a F4 problem or a real problem in your environment/setup.

As a general rule: to use Java FFI you must have your Java code in another pod because the Java code must already be compiled. To mix Java and Fantom in one pod requires using native classes/methods.

For quick and simple I think using Java FFI is easier. But if you really want a clean integration using native classes gives you the most flexibility to develop a nice Fantom API which leverages Java APIs - look at concurrent APIs as really good example.

jhughes Tue 29 Jan 2019

The concurrent pod looks to be a good example of what i'm trying to do so i've created the most basic pod. One native fantom class with a make command and a java class with the make method and an empty constructor. F4 still doesn't know what to do with this so I need to attempt a build from the command line. Always relied on the IDE to handle the build.fan scripts so I'm not really sure what this means for a command line. Couldn't figure this out from the build documentation.

jhughes Tue 29 Jan 2019

For completeness, here is the contents of the build.fan file. When I navigate to where this folder is and run

fan build.fan

I get no output, no errors, no messages, just nothing.

using build

class Build : BuildPod {

 new make() {
podName = "nativeTest"
summary = "My Awesome NativeTest Project"
version = Version("1.0")

meta = [
	"proj.name" : "NativeTest"
]

depends = [
	"sys 1.0"
]

srcDirs = [`fan/`]
resDirs = [,]
javaDirs = [`java/`]

docApi = true
docSrc = true
 }
}

brian Wed 30 Jan 2019

I get no output, no errors, no messages, just nothing.

If that fan command is mapped to the batch/bash script correctly then I don't think its possible for you to get nothing. Assuming you are windows, try using the full path the batch file bin/fan.bat.

Also other things to try:

  1. using the -? which will print out something like below
  2. using the -dumpEnv
  3. using the -v which will dump a lot of details to stdout

    /work/fan/src> fan concurrent/build.fan -? usage:

    build [options] <target>*

    options:

    -? -help       Print usage summary
    -v             Verbose debug logging
    -dumpEnv       Debug dump of script env

    targets:

    compile*       Compile to pod file and associated natives
    clean          Delete all intermediate and target files
    test           Run the pod unit tests via fant
    full           Run clean, compile, and test

jhughes Wed 30 Jan 2019

I am working with windows and was hitting some weird permissions issues when attempting any of that and then switched computers and those issues went away. Then I found it seemed to be having issues if there were spaces in the path to the build file/project files as then it would fail on being able to resolve the java files. Changed the path to to have no spaces in folder names and it would compile. The issues still remain in F4 however so there's probably some additional settings to get right there that I won't likely be investigating but if anybody else has the answers to compiling native code with the IDE, i'd love to hear how you accomplished that.

Login or Signup to reply.