tweag/inline-java

how to run inline-java by runghc?

clojurians-org opened this issue · 2 comments

i use nix-shell to setup the inline-java environment,
and i also can ghc compile and run the file.

but i have no idea how to use 'runghc' to run it.
and i want to know whether it can be support to do?

[larluo@nixos-larluo:~/my-repo/datahub/contrib/metadata-ingestion]$ nix-shell ./bin/dataset-jdbc-generator.hs.nix

[nix-shell:~/my-repo/datahub/contrib/metadata-ingestion]$ runghc ./bin/dataset-jdbc-generator.hs

bin/dataset-jdbc-generator.hs: error:
    inline-java Plugin: found invalid qqMarker.

<no location info>: error: 
Compilation had errors


*** Exception: ExitFailure 1

the related file is:

[nix-shell:~/my-repo/datahub/contrib/metadata-ingestion]$ cat bin/dataset-jdbc-generator.hs
#! /usr/bin/env nix-shell                                                                                                                                                     
#! nix-shell dataset-jdbc-generator.hs.nix -i runghc                                                                                                                                

{-# LANGUAGE OverloadedStrings, FlexibleInstances, FlexibleContexts, ScopedTypeVariables #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}

import Language.Java (withJVM)
import Language.Java.Inline

main :: IO ()
main = withJVM [] [java| {
  System.out.println("Hello Java!") ;
} |]

[nix-shell:~/my-repo/datahub/contrib/metadata-ingestion]$ cat bin/dataset-jdbc-generator.hs.nix
with import <nixpkgs> {} ;                                                                                                                                                    
let                                                                                                                                                                           
  inline_java_git = fetchFromGitHub {                                                                                                                                         
      owner = "tweag" ;                                                                                                                                                       
      repo = "inline-java" ;                                                                                                                                                  
      rev = "a897d32df99e4ed19314d2a7e245785152e9099d" ;                                                                                                                      
      sha256 = "00pk19j9g0mm9sknj3aklz01zv1dy234s3vnzg6daq1dmwd4hb68" ;                                                                                                       
  } ;                                                                                                                                                                         
  haskellPackages = pkgs.haskellPackages.override {                                                                                                                           
    overrides = self: super: with pkgs.haskell.lib; {                                                                                                                         
      jni = overrideCabal (self.callCabal2nix "jni" (inline_java_git + /jni) {}) (drv: {                                                                                      
        preConfigure = ''                                                                                                                                                     
          local libdir=( "${pkgs.jdk}/lib/openjdk/jre/lib/"*"/server" )                                                                                                       
          configureFlags+=" --extra-lib-dir=''${libdir[0]}"                                                                                                                   
        '' ;                                                                                                                                                                  
      }) ;                                                                                                                                                                    
                                                                                                                                                                              
      jvm = overrideCabal (self.callCabal2nix "inline-java" (inline_java_git + /jvm) {}) (drv: {                                                                              
        doCheck = false ;                                                                                                                                                     
      }) ;                                                                                                                                                                    
      inline-java = overrideCabal (self.callCabal2nix "inline-java" inline_java_git {}) (drv: {
        doCheck = false ;                  
      }) ;                                 
    } ;                                    
  };                                       
                                           
in
mkShell {
  buildInputs = [
    pkgs.jdk
    (haskellPackages.ghcWithPackages ( p: 
      [ p.bytestring p.string-conversions
        p.exceptions 
        p.inline-java
      ]
    ))
  ];
}

Hello @clojurians-org. This seems to work:

$ runghc --ghc-arg=-fobject-code test.hs
Hello Java!

I had to modify your file slightly to use a bound thread:

{-# LANGUAGE OverloadedStrings, FlexibleInstances, FlexibleContexts, ScopedTypeVariables #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}

import Control.Concurrent
import Language.Java (withJVM)
import Language.Java.Inline

main :: IO ()
main = Control.Concurrent.runInBoundThread $ withJVM [] [java| {
  System.out.println("Hello Java!") ;
 } |]

it works, thanks so much