-
Notifications
You must be signed in to change notification settings - Fork 4
install
There are two different uses of ScriptBasic for Java. The simpler one when you just want to start some BASIC program from the command line. The binary package of ScriptBasic is a Java JAR file that contains an appropriately configured main class that can be executed. The other use is when you want to invoke BASIC scripts from some application and you want to use ScriptBasic for Java as an embedded script language interpreter.
PUT IT ON THE CLASSPATH!!!
Check that the Java runtime environment (JRE) is installed on your system. To do that you can execute the command
java -version
and you should get some output like
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
The version of the build number may be different but make sure that the java version you use is 11 or later. SB4J requires java 11.
Locate the SB4J JAR file and the BASIC program you want to execute. Make sure that the BASIC program
is in a file that has the extension sb
or bas
. Execute the following command line
java -jar jscriptbasic-x.y.z.jar myProgram.sb
where x.y.z
is the version of the interpreter and is part of the file name of the jar file in the
distribution. For the hello world example on the development server, I execute
java -jar target/jscriptbasic-2.1.1.jar src/test/basic/hello.sb
which will print hello
.
Place the JAR file on the classpath and use the JSR-223 standard interface to invoke ScriptBasic or use the ScriptBasic for Java native interface.
Place the JAR file on the classpath and use the native interface
ScriptBasic engine = ScriptBasic.engine();
engine.eval("print \"hello world\"");
to evaluate some BASIC. ... more on the native api.
SB4J is compliant with the JSR-223 standard. If you use this interface to integrate ScriptBasic into your application you need to know anything that is specific to jScriptBasic. All you have to do it copy the JAR file to a location so that the Java class loader will find it. After that start up your application and the JRE will load ScriptBasic automatically using the standard ServiceLoader interface.
To get a fast track to integrate jScriptBasic into an application you can have a look at the source code that implements the command-line version of the interpreter and embeds the interpreter into a 'main' class.
The code is in the package com.scriptbasic.main
and the class is CommandLine
. The important
part is:
final PrintWriter output = new PrintWriter(System.out);
final PrintWriter error = new PrintWriter(System.err);
try {
final InputStreamReader input = new InputStreamReader(System.in);
final Context ctx = ContextBuilder.from(new FileReader(basicProgramFileName),
input, output, error);
ctx.interpreter.registerFunctions(FileHandlingFunctions.class);
registerSystemPropertyDefinedClasses(ctx);
ctx.interpreter.setProgram(ctx.syntaxAnalyzer.analyze());
ctx.interpreter.execute();
} catch (final Exception exception) {
Throwable cause = Optional.ofNullable(exception.getCause()).orElse(exception);
if (cause.getMessage() != null) {
System.err.println("ERROR: " + cause.getMessage());
} else {
throw exception;
}
} finally {
output.flush();
error.flush();
}
The code creates a new javax.script.ScriptEngineManager
that manages the loading and the location
of the interpreter. The command-line version of ScriptBasic for Java asks the script engine manager to create
an interpreter based on the file name extension. As a side effect, you can also execute JavaScript programs from
the command line using the jScriptBasic JAR. The JavaScript interpreter is included in the JRE since the version
1.7
The following few lines set the standard and error input and output for the program and finally, the script is executed. At the end, the output is flushed and closed so that the characters that were printed by the BASIC program are sent out to the console.
To have the interpreter on the command line you can manually copy the JAR file to the classpath, or you can add the JAR file to the build process. If you use maven use the following dependency:
<dependency>
<groupId>com.scriptbasic</groupId>
<artifactId>jscriptbasic</artifactId>
<version>2.1.1</version>
</dependency>
At the moment the latest release is 2.1.1
. For the actual state of the different releases
have a look at the page releases.
Starting on the page you will find a detailed tutorial on how to embed ScriptBasic for Java using the JSR223 standard interface.
The advanced use of ScriptBasic for Java invoking the special functions that are not available through the JSR223 standard interface are detailed in a series of separate pages here.