- Ajuda do Scilab
- Java from Scilab
- Getting started - Beginning
- Getting started - Second step
- jallowClassReloading
- jarray
- jautoTranspose
- jautoUnwrap
- jcast
- jcompile
- jconvMatrixMethod
- jcreatejar
- jdeff
- jdisableTrace
- jenableTrace
- jexists
- jgetclassname
- jgetfield
- jgetfields
- jgetinfo
- jgetmethods
- jimport
- jinvoke
- jinvoke_db
- jnewInstance
- jnull
- jremove
- jsetfield
- junwrap
- junwraprem
- jvoid
- jwrap
- jwrapinchar
- jwrapinfloat
- new
Please note that the recommended version of Scilab is 2025.0.0. This page might be outdated.
See the recommended documentation of this function
Getting started - Beginning
How to use the Java Scilab binding?
Description
The goal is this module is to allow the load and interaction on Java objects and datatypes.
Basic
Before starting, it is convenient to know the most important functions and what they do. These frequently used functions are the following Scilab functions:
jimport is the function that mirrors the functionality of the java statement 'import', and loads the specified class definition/template of a given class in memory. When loaded, this definition is used to accesses static methods/members, and create new objects.
jinvoke is a function that calls (invokes) a specified method on a java class or object. This invoke has an optional set of parameters that must coincide with the actual member signature. This means you must pass the same number of arguments, and these arguments must have the correct type.
Example 1: Creating a basic class and calling a simple method
In this first example, the three basic pillars of working with Java are treated. The first is to load a class, then an instance is constructed and finally the invocation or calling of one of this methods or members.
Consider a basic class as presented in the example HelloWorld
. It has a default constructor generating a message upon construction and one public method which also shows a message when it has been called. This class should now be compiled into java byte-code. When developing your own code, then this section is usually handled by your IDE (integrated development environment). If you plan to use external libraries, these will available in precompiled form (packed in a JAR).
// Save under the name HelloWorld.java package com.foo; public class HelloWorld { public HelloWorld() { System.err.println("HelloWorld constructed!"); } public void message() { System.err.println("Hello world!"); } }
// How to compile the Java code from Scilab javacode=mgetl(fullfile(TMPDIR, 'HelloWorld.java')); jcompile("com.foo.HelloWorld",javacode);
Once the compiled version of this Java class exists, we can start Scilab and try to get Scilab to show us the various messages. Now the HelloWorld class can be imported into our workspace. This is done using the already mentioned jimport:
-->jimport com.foo.HelloWorld -->HelloWorld HelloWorld = class com.foo.HelloWorld -->whos -name HelloWorld Name Type Size Bytes HelloWorld _EClass ? 168
Upon competition, an additional variable named HelloWorld has been created. This is equivalent to a Class object in java. From this class object, new objects of the HelloWorld type can be created.
Creating such an object instance can be done by invoking new on the class definition. The arguments to this function are the parameters that are delegated to the Java constructor. The result of this operation is a new Java object reference which can be stored in a variable for later use.
-->object = HelloWorld.new(); HelloWorld constructed! -->object object = com.foo.HelloWorld@49aacd5f -->whos -name object Name Type Size Bytes object _EObj ? 160
What one sees when the new operator is called on the JClass, it transparently invokes the Java constructor, and our "HelloWorld constructed!" message appears. The resulting HelloWorld object is stored in the "object" variable. When the variable name is reentered in the command line, the details of its reference are shown. This message can be customized by overriding the toString method in the HelloWorld class.
Now that a specific HelloWorld object has been created, one can try to call the public method that has been declared; HelloWorld\#message()
. The same natural technique as with new can then be applied to invoke the method:
-->object.message(); Hello world!
The dot operator (dot between object and message) is actually a handy shortcut and expands the following snippet of Scilab code. The use of this shortcut makes it simpler and cleaner to invoke methods/get member variables.
-->jinvoke(object, 'message'); Hello world!
Example 2: Exchanging Scilab and Java primitives
This example treats the way you can exchange primitive data types and strings between Scilab and Java. We will be passing various types of objects between these two languages.
For this an example class (see Class Inspector) has been defined that takes and returns objects. There are two methods defined. The first takes a double does some arithmetic and returns a result: Inspector#eval(double). The other methods takes any object, shows some basic information and returns it: Inspector#inspect(Object).
// Save under the name Inspector.java package com.foo; public class Inspector { public double eval(double x) { return x / 2.; } public Object inspect(Object prototype) { System.err.println("Inspecting: '" + prototype + "'"); System.err.println("Class: " + prototype.getClass().getSimpleName()); return prototype; } }
As in the previous example, this code must be compiled to Java byte-code before it can be used directly.
// How to compile the Java code from Scilab javacode= mgetl(fullfile(TMPDIR, 'Inspector.java')); jcompile("com.foo.Inspector",javacode);
-->jimport('com.foo.Inspector'); -->myInspector = Inspector.new() myInspector = com.foo.Inspector@2a788315
-->result = myInspector.eval(12.5) result = 6.25 -->result * 2 ans = 12.5 -->whos -name result Name Type Size Bytes result constant 1 by 1 24
-->result = myInspector.eval(12.5) result = 6.25 -->result * 2 ans = null -->whos -name result Name Type Size Bytes result _EObj ? 160
-->result = junwrap(result) result = 6.25 -->whos -name result Name Type Size Bytes result constant 1 by 1 24 -->result * 2 ans = 12.5
-->jautoUnwrap(%f) // Make sure we disable the auto Unwrap -->result = myInspector.inspect("Hello world!"); Inspecting: 'Hello world!' Class: String -->whos -name result Name Type Size Bytes result _EObj ? 160 -->result = junwrap(result) result = Hello world! -->whos -name result Name Type Size Bytes result string 1 by 1 72 // An Integer -->result = myInspector.inspect(int32(150)); Inspecting: '150' Class: Integer -->result = junwrap(result) result = 150 -->whos -name result Name Type Size Bytes result int32 1 by 1 40 // A boolean -->result = myInspector.inspect(%t); Inspecting: 'true' Class: Boolean -->result = junwrap(result) result = T -->whos -name result Name Type Size Bytes result boolean 1 by 1 16
--> jautoUnwrap(%t) // Make sure we come back in the default mode where Scilab auto unwrap all calls -->result = myInspector.inspect(1:5) Inspecting: '[D@b05236' Class: double[] result = 1. 2. 3. 4. 5. -->whos -name result Name Type Size Bytes result constant 1 by 5 56 -->result = myInspector.inspect(testmatrix('magi',3)) Inspecting: '[[D@11d13272' Class: double[][] result = 8. 1. 6. 3. 5. 7. 4. 9. 2. -->whos -name result Name Type Size Bytes result constant 3 by 3 88
History
Versão | Descrição |
5.5.0 | Function introduced. Based on the 'JIMS' module. The main difference in the behavior compared to the JIMS module is that jautoUnwrap is enabled by default. |
Report an issue | ||
<< Java from Scilab | Java from Scilab | Getting started - Second step >> |