[Changeset] Re: [OctDev] Java calling Octave via JNI
David Bateman
David.Bateman at motorola.com
Fri Sep 5 03:41:07 CDT 2008
David Bateman wrote:
> MartinHepperle wrote:
>> Hello all,
>>
>> again this nasty Java -> Octave connection.
>>
>> I also want to embed Octave into a Java program and call Octave
>> functions
>> from Java.
>>
>> Initially I started with programming an execution engine which starts
>> octave.exe and connected to its standard input and output pipes. This
>> concept works (in the meantime I also found two solutions which
>> implement
>> the same technique) but is somewhat ugly and inefficient due to
>> conversions
>> to and from strings
>>
>> What I tried now was to call the Octave DLLs (Windows platform)
>> directly via
>> a C wrapper and using JNI.
>> This technique provides a very elegant solution and I have used this
>> in the
>> past to call C and FORTRAN codes from Java. I want to define some global
>> variables, set the values from Java, execute some user supplied
>> functions
>> and finally return the results (global variables set inside the user
>> functions) to Java.
>>
>> Now with the Octrave DLLs I stumbled across a problem.
>>
>> To study the interface to ther DLLs I started with a C++ program.
>> Basically I used the following logic:
>>
>> //-----------------------------
>> // start and initialize Octave (once, then keep it alive)
>> char * pArgs[] = {"octave.exe","--path", "C:\\Programme\\Octave",
>> "--norc",
>> NULL};
>> int iRet = octave_main (4 pArgs, true);
>>
>> //-----------------------------
>> // execute some commands
>> std::string s = std::string("global Output;\nglobal
>> Input;\nInput=12;\nOutput=2*Input;\n");
>> octave_value x = eval_string (s, false, parse_status);
>> // examine the returned "x" with x.isnumeric() etc. works when code
>> is in an
>> executable (.exe) file
>>
>> // what does the follwing code do? It works, but where is the
>> variable in
>> Octave?
>> set_global_value (std::string(cName), val);
>> octave_value vReturn = get_global_value (std::string(cName));
>> // the variable has the correct value, but is_globally_visible
>> (std::string(Name)) returns false...
>> // what does global mean here?
>>
>>
>>
>> // terminate Octave
>> do_octave_atexit();
>> //-----------------------------
>>
>> All this works well when I have the code inside an executable C++
>> program
>> (MSVC8.0).
>>
>> So far so good.
>>
>> Now for the interface via JNI I have to put this into a DLL which
>> provides a
>> thin layer between Java objects and the C interface to Octave.
>> If I put the same code inside a DLL I can initialize Octave, but the
>> eval_string () and other functions doe not work - they seem to do
>> nothing. I found can do somethings with set_global_value () and
>> get_global_value (),
>> but this is not enough and also these "global" values seem to be
>> something
>> else than global variables in Octave.
>>
>> Anyway, I see that eval_string() seems to work when called from a C
>> executable, but not when I call it from a DLL. Could this have
>> something to
>> do with i/o streams which are different in a DLL and in an EXE?
>>
>> Any ideas where to look and how to test?
>>
>>
>> Thank You,
>>
>> Martin
>>
>>
>>
>>
>>
> eval_string can only work if the interpreter is initialized.. Check
> the mailing lists for how to initialize the interpreter.
>
> D.
>
>
Seeing as how this question has come up a number of times, I suggest the
attached changeset that adds an example of how to initialize the
interpreter and use it from a standalone application.
Regards
David
--
David Bateman David.Bateman at motorola.com
Motorola Labs - Paris +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin +33 6 72 01 06 33 (Mob)
91193 Gif-Sur-Yvette FRANCE +33 1 69 35 77 01 (Fax)
The information contained in this communication has been classified as:
[x] General Business Information
[ ] Motorola Internal Use Only
[ ] Motorola Confidential Proprietary
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patch8500
Url: https://www-old.cae.wisc.edu/pipermail/octave-maintainers/attachments/20080905/f1bcf29b/attachment.ksh
More information about the Octave-maintainers
mailing list