Skip to content
/ jbang Public
forked from jbangdev/jbang

Unleash the power of Java - JBang Lets Students, Educators and Professional Developers create, edit and run self-contained source-only Java programs with unprecedented ease.

License

Notifications You must be signed in to change notification settings

ge0ffrey/jbang

Repository files navigation

j’bang - Having fun with Java scripting

Release Build Status

Ever tried out Java 10+ support for running .java files directly in your shell but felt it was a bit too cumbersome ?

Then try jbang which gives you:

  • Dependency declarations using //DEPS <gav> for automatic dependency resolution

  • Dependency resolution caching

  • (PLANNED) Launch with debug enabled for instant debugging from your favorite IDE

  • (PLANNED) Generate gradle/maven file with dependencies for easy editing in your favorite IDE

  • (PLANNED) Lookup dependencies with a short-hand name, i.e. // DEPS log4j:1.2+,picocli for quick getting started.

To use it simply install jbang and run jbang yourscript.java

Installation

To use jbang you need to have Java 11+ available.

Manual Install

Unzip the latest binary release, put the jbang-<version>/bin folder in to your $PATH and you are set.

Homebrew

On OSX you can install jbang with Homebrew using maxandersen/tap.

brew install maxandersen/tap/jbang

To upgrade to latest version:

brew upgrade maxandersen/tap/jbang

Script input

A script is just a single .java file with a classic static main method.

Below is an (almost) minimal example you can save in helloworld.java:

//usr/bin/env jbang "$0" "$@" ; exit $? (1)

class helloworld { (2)

    public static void main(String[] args) {
        if(args.length==0) {
            System.out.println("Hello World!");
        } else {
            System.out.println("Hello " + args[0]);
        }
    }
}
  1. By using this // style instead of shebang !# you trick bash, zsh etc. to run this as a script while still being valid java code.

  2. A classname, can be anything when using jbang but to be valid java for most IDE’s you’ll want to name it the same as the source file.

Now to run this you can call it via jbang:

jbang helloworld.java

or mark it executable and just run it directly:

chmod +x helloworld.java
./helloworld jbang!

Declare dependencies with //DEPS

If you want to write real scripts you will want to use some java libraries. To specify dependencies you use gradle-style locators. Here is an example using log4j

//usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS log4j:log4j:1.2.17 (1)

import static java.lang.System.out;

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

import java.util.Arrays;

class classpath_example {

	static final Logger logger = Logger.getLogger(classpath_example.class);

	public static void main(String[] args) {
		BasicConfigurator.configure(); (2)
		logger.info("Welcome to jbang");

		Arrays.asList(args).forEach(arg -> logger.warn("arg: " + arg));
		logger.info("Hello from Java!");
	}
}
  1. //DEPS has to be start of line and can be one or more space separated dependencies.

  2. Minimal logging setup - required by log4j.

Now when you run this the first time with no existing dependencies installed you should get an output like this:

$ ./classpath_example.java
[jbang] Resolving dependencies...
[jbang]     Resolving log4j:log4j:1.2.17...Done
[jbang] Dependencies resolved
0 [main] INFO classpath_example  - Welcome to jbang
1 [main] INFO classpath_example  - Hello from Java!

FAQ

  1. Why the name j’bang?

    I was reading up on how to use the new shebang (#!) feature support in Java 10 and came up with the idea of port kscript to Java and needed a name. From there came j’bang which is a "bad" spelling of how shebang is pronounced in french.

  1. Why use of gradle resource locators rather than ?

    kscript used it and its nice as it is a one-liner and easily parsable.

  1. Why would I use Java to write scripts ? Java sucks for that…​

    Well, does it really ? With Java 8 streams, static imports and greatly improved standard java libraries it is very close to how kscript and grape looks like. With the following advantages:

    • works with plain Java without installing additional compiler/build tools

    • all ide’s support editing .java files very well, content assist etc.

    • great debugging

And to be honest I built jbang just to see if I could and get my Java skills refreshed for the newer features in the language. Use it at your own risk :)

Thanks

jbang was heavily inspired by how kscript by Holger Brand works.

About

Unleash the power of Java - JBang Lets Students, Educators and Professional Developers create, edit and run self-contained source-only Java programs with unprecedented ease.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 90.6%
  • Shell 5.0%
  • Gherkin 1.9%
  • PowerShell 1.2%
  • Batchfile 0.6%
  • Smarty 0.4%
  • Other 0.3%