1

I would like to execute my javascript programs with the Rhino shell without making the first line #!/bin/sh. '#' isn't a comment character in javascript. I also don't want to have to have a .js extension.

Is this possible? I remember reading something a long while about a method of making java programs to run without an explicit interpreter. Google has not been forthcoming, however.

2 Answers 2

2

You'll want to use the kernel-module binfmt_misc for this. I don't have a JS interpreter installed, but I guess the magic would be something like this for Rhino:

# If it's not included in your kernel
sudo modprobe binfmt_misc
sudo mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc

# Register the .js extension as being run by Rhino
echo ':JSScript:E::js::/path/to/rhino:' > /proc/sys/fs/binfmt_misc/register

This assumes that your scripts are happy to be run like:

 /path/to/rhino /path/to/your/script

If you need to pass any arguments to Rhino, then you'll need to set up a wrapper. Full documentation for binfmt_misc is here.

Sign up to request clarification or add additional context in comments.

2 Comments

And don't forget to make the script executable.
Since I wanted to be able to name my scripts without the file extension, I also added:echo ':javascript:M:://!::/usr/bin/js:' > /proc/sys/fs/binfmt_misc/register
0

Yes, simply call rhino /path/to/your/js/file

In case you don't have a program rhino in your path, replace it with java org.mozilla.javascript.tools.shell.Main

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.