Upload files by relative path to current file in Rhino

I am writing a JavaScript program in Rhino that should load other JavaScript files. However, the built-in function load()loads the files relative to the current directory , and I need to download them relative to the location of the script (so that the program can be called from any directory).

In other languages ​​I would use something like dirname(__FILE__) + "/path/file", but it seems that Rhino does not __FILE__or something like that. I tried to extract the current file from the generated exception, but it is empty, i.e. The following code prints "true":

try {
  throw new Error();
} catch (e) {
  print(e.fileName === "");
}

I tried to look at the sources of the interpreter and use the Java-JavaScript bridge, but so far have not found anything useful (I will probably look more).

Does anyone have a clue how to get files to be uploaded to relative paths?

+3
source share
1 answer

Probably the easiest way to run the script launcher. If you are using * nix or OS X, you can put the shell script in the same directory as all of your Javascript, which changes the directory to your script directory before running:

#!/bin/sh
cd `dirname "$0"`
java -jar js.jar your_script.js

If your script should be running in the current user directory, you can instead wrap its location on the command line:

#!/bin/sh
DIR=`basename "$0"`
java -jar "$DIR/js.jar" "$DIR/loader.js" "$DIR"

Then, in loader.js, use the Rhino builtin argument variables to access "$ DIR":

load(arguments[0] + "/another_js_file.js");

Rhino features are documented here .

Windows; CMD.EXE.

+3

Source: https://habr.com/ru/post/1733677/


All Articles