Project bashj : a bash mutant with java support

De Lillois Fractale Wiki
Aller à la navigation Aller à la recherche

What ?

The bashj project is hosted on sourceForge.

bashj is an extended bash allowing to use java libraries functions and code in bash scripts.

Java Shell Bridge is the underlying technology used by bashj. It is mainly a server executing java code for the benefit of a bash session (interactive or scripted).

june 2018 - this is still a beta release

Why ?

Java and bash are often partners in software projects.

Personnally I used both of them widely. Being rather lazy, I tried to minimize the number of necessary techonologies in my projects. These two covered 99% of my needs together. Java is perfect to translate concepts into software. But bash is necessary for various OS level tasks, like process control, deployment ... But I was frequently disappointed by the incapacity to reuse java results and expertise from scripts (this was only possible with java process launching, and main() entry points). So I decided to set up the tool described here... 

Their possible interactions are numerous but limited in terms of integration.

With this project, any script is given the capacity to quicly and directly call java methods without creating new OS processes.

How ?

The JSBServer (a java daemon process)  communicates with bash scripts.

Specialized bash function (jbs() and jsbXXX()) are defined. They are used from bash scripts to request server actions.

The communication uses two named pipes.

All this may be also be seen as a bash preprocessor.

Limitations & 

  • bash only
  • Linux only
  • java 9+
  • public static methods only
  • methods with primitive types (including String) as parameters and return value
  • no varargs

Installation

Follow carefully these steps:

  1. create a directory  :  sudo mkdir -m=777  /var/lib/bashj/
  2. download from  sourceForge the jsbInstall.jar installation file and move it in this directory
  3. go to the installation directory : cd /var/lib/bashj/
  4. run in bash : jar xvf  jsbInstall.jar
  5. run in bash : sudo ./jsbInstall

Check try test

Low level testing : the jsb bridge

After the installation run these commands in a bash terminal. Their output should be self explanatory...

. jsbInit   # the dot ( . )  is important - this loads various bash functions into the current bash session
jsbHelp
jsbStart
jsbStatus
jsbClasses
jsbMethods
jsbFields
echo $(jsb Math.hypot 3.0 4.0)
echo $(jsb Math.PI)
jsbTest

Testing bashj

cat /var/lib/bashj/example/ex1
bashj < /var/lib/bashj/example/ex1

It is suggested to try, copy, adapt some examples scripts present in the example subdiretory

bashj syntax rules and java calls

The bashj is only available in scripts. It may not be used for an interactive session.

A bashj file is similar to a standard bash file. Just replace the shebang : #!/bin/bashj instead of #!/bin/bash.

Three families of java methods & fields may be called

  • Those defined in the beginning of the running bash script itself, between a line containing #@javaStartand a line containing #@javaEnd. Java methods are define there without package name and without class name. The implicit method name is "j". So a function public static int factorial(int n) may be called in the script body as j.factorial(int).
  • Those present in the standard java packages java.lang.System , java.lang.String and java.lang.Math
  • Those present in jars put by the user in the subdirectory jarlib.

Methods may be called with various equivalent syntax. The following lines (in a bash script) are equivalent:

  • echoMath.hypot(3.0,4.0)
  • echo java.lang.Math.hypot(3.0,4.0)
  • echo $(jsb Math.hypot 3.0 4.0)

The first one is the most readable and recommended.

Configuration

No configuration is necessary.

However it is possible for the user to put various jar files under the jarlib directory.

All public static methods and all public static fields defined in these classes will be available in the bridge.

They should be called as indicated before.

Annexes

Support

Please contact fil for support, remarks, suggestions,...

Installed Files

The files are installed under /var/lib/bashj/, and include:

  • JSBServer.jar (a jar containing the compiled java class JSBServer.class)
  • jsbInit (a set of bash functions) (a link is created in /usr/bin)
  • bashj (a script, the command interpreter) (a link is created in /usr/bin)
  • README a readme file (a short version of this page)
  • pipe/a subdirectory used for named pipes
  • classes/ a subdirectory with transient compiled java classes
  • jarlib/ a subdirectory with the jars choosen by the user to make the involved classes available   
  • example/ a subdirectory with various example scripts