Java package

View: New views
20 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

Java package

by xolotl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Is there really _no_ documentation, not even a few examples?

Thanks,

-- O.L.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by Michael Goffioul-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Jan 23, 2008 9:11 PM, Olivier Lefevre <lefevrol@...> wrote:
> Is there really _no_ documentation, not even a few examples?

What do you mean *no* documentation? There is a function reference:
http://octave.sourceforge.net/doc/funref_java.html.

If you want to see a real example, look at JHandles m-code.

Michael.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by xolotl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> What do you mean *no* documentation? There is a function reference:
> http://octave.sourceforge.net/doc/funref_java.html.

Yes I found the function reference but one also expects a document,
even if just one page, putting it all together.

> If you want to see a real example, look at JHandles m-code.

Thanks. I will install it and have a look but this is a perfect
example of why better documentation is needed: I had no idea I
also needed the JHandles package and I don't see anything in the
java-1.2.2.tar.gz package that suggests it.

For memory and just to clarify, I am trying to call Octave from
Java, not the other way round.

Thanks,

-- O.L.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by Michael Goffioul-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Jan 24, 2008 3:22 PM, Olivier Lefevre <lefevrol@...> wrote:

> > What do you mean *no* documentation? There is a function reference:
> > http://octave.sourceforge.net/doc/funref_java.html.
>
> Yes I found the function reference but one also expects a document,
> even if just one page, putting it all together.
>
> > If you want to see a real example, look at JHandles m-code.
>
> Thanks. I will install it and have a look but this is a perfect
> example of why better documentation is needed: I had no idea I
> also needed the JHandles package and I don't see anything in the
> java-1.2.2.tar.gz package that suggests it.

You got me wrong. You don't need JHandles to run the java package.
JHandles *uses* the functions provided by the java package. So if
you wanted an example, that's a good one.

> For memory and just to clarify, I am trying to call Octave from
> Java, not the other way round.

That's not the intentation of the java package. This package is an
interface for accessing Java from Octave. This is done through the
functions java_new, java_invoke, java_get and java_set, which are
documented. The rest is not documented.

Michael.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by xolotl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> You got me wrong. You don't need JHandles to run the java package.
> JHandles *uses* the functions provided by the java package. So if
> you wanted an example, that's a good one.

OK.

>> For memory and just to clarify, I am trying to call Octave from
>> Java, not the other way round.
>
> That's not the intention of the java package. This package is an
> interface for accessing Java from Octave.

I had been clear about this when I asked the first time, though
(on the general Octave mailing list). So, back to the drawing board:
is there a ready-made solution for calling Octave from Java, or
even from C? I can write JNI code if I must, I just don't know
Octave internals and don't wish to have to figure them out.

Regards,

-- O.L.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by Michael Goffioul-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Jan 24, 2008 3:57 PM, Olivier Lefevre <lefevrol@...> wrote:
> I had been clear about this when I asked the first time, though
> (on the general Octave mailing list).

Sorry, I didn't remember (so many mails, I don't remember everything).

> So, back to the drawing board:
> is there a ready-made solution for calling Octave from Java, or
> even from C? I can write JNI code if I must, I just don't know
> Octave internals and don't wish to have to figure them out.

What I stated previously still holds. The java package provides
some way (through the Octave.java class) for java code to
access octave. However, and that's the crucial point, the
Octave.java class assumes that octave engine is already running
and that the java package (from octave-forge) has been
initialized. So it's designed to work in the following way:
octave -> java -> octave.

This part of the java package is internal, so don't expect to find
any documentation about it. However, I can give you already an
idea about how it's working. Let's assume octave is running:
1) octave wants to make a call to some Java code
2) octave initializes the java package
  2a) the JVM is started (and the octave thread ID is stored internally)
  2b) the java package installs an input_event_hook function that is
        called regularly by readline (== when octave is idle)
  2c) octave transfer the call to the JVM (actually to the
        ClassHelper.java class, that takes care of finding and calling
        the java method)
3) let's state now that the (called-from-octave) java code wants to
    make a call in octave; it uses the Octave.java class for that and
    for instance call evalAndWait(String)
4) if the current thread is the same as the octave thread, the call
    is executed immediately (through JNI); if it's another thread, the
    call is put on a stack and the calling thread is stopped
5) the input_event_hook installed in 2b checks if there are any pending
    call to execute and executes them; finally it notifies any waiting thread
6) the calling thread in 4 resumes

This implementation has several limitations:
1) it assumes octave is running and the java package is initialized
2) there's no mechanism to get the return value of octave calls back
    to octave

What you're looking for (calling directly octave from a java application)
is currently not possible with the existing code; however I think it can
serves as a starting point to add this functionality. What is missing:
1) a way to initialize the octave engine without starting the standard
read-eval-print loop; there's currently no API to do that.
2) the __java__.cc file (and probably some java classes) should be
adapted in order to initialize the internal variables with the running
JVM (== the running java application).

You're not the only one to want this feature. See for instance this
thread: http://www.nabble.com/How-to-initialize-octave-without-read-eval-print-loop--to14261528.html#a14494130

I don't have time to work on that, but I'd be glad to integrate patches
that would implement it.

Michael.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by xolotl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> You're not the only one to want this feature. See for instance this
> thread: http://www.nabble.com/How-to-initialize-octave-without-read-eval-print-loop--to14261528.html#a14494130

And I note that Eaton did not answer your question...

-- O.L.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by xolotl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm looking at the source now. If you want the equivalent of
the octave_main function from src/octave.cc without the call
to main_loop, doesn't the embedded argument to octave_main
already let you do that?

-- O.L.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by Michael Goffioul-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 1/24/08, Olivier Lefevre <lefevrol@...> wrote:
> I'm looking at the source now. If you want the equivalent of
> the octave_main function from src/octave.cc without the call
> to main_loop, doesn't the embedded argument to octave_main
> already let you do that?

Indeed, I overlooked that. It might then be easier than I thought.
If you're willing to implement it and you succeed, I'll integrate your
changes in the java package.

Michael.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by Michael Goffioul-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Jan 24, 2008 10:16 PM, Michael Goffioul <michael.goffioul@...> wrote:
> On 1/24/08, Olivier Lefevre <lefevrol@...> wrote:
> > I'm looking at the source now. If you want the equivalent of
> > the octave_main function from src/octave.cc without the call
> > to main_loop, doesn't the embedded argument to octave_main
> > already let you do that?
>
> Indeed, I overlooked that. It might then be easier than I thought.
> If you're willing to implement it and you succeed, I'll integrate your
> changes in the java package.

If it can help you, here are some hints about how I would
do it:
- don't re-use the Octave.java class; it has been designed to
  work with JHandles in order to execute callbacks synchronously
  with an interactive octave.
- don't re-use the input_event_hook, this is mainly useful when
  you want to synchronize with an interactive octave, which is
  not what you want.
- don't try to initialize the java package (by calling java_init for
  instance); this will be done automatically by the octave engine
  when it needs it; the java package is able to attach itself to
  a running JVM, instead of starting a new one; however there
  are some java properties that would need to be defined by
  the calling java application
- define a new OctaveEngine class
- add a native start method to start the octave engine: calls
  octave_main with embedded set to true
- add native methods to call octave functions, eval strings...
  (there you can re-use the box/unbox functions to perform
  octave/java object conversion; conversion is controlled
  internally by Vjava_convert_matrix and Vjava_unsigned_conversion)
- because octave is not thread-safe, you must make sure the
  octave functions are called by a single thread at a time;
  this can be achieve from java world by using the synchronized
  keyword, for instance:

public class OctaveEngine
{
  static Object __lock__ = new Object();

  Object call_function (String name, Object[] args)
  {
    synchronized (__lock__)
    {
      // do something
    }
  }

  void eval_string (String cmd)
  {
    synchronized (__lock__)
    {
      // do something
    }
  }
}

Michael.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by xolotl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks. Two questions:
1) in a scenario where Java is in charge, where does something
    like the existing java package fit in?
2) I fear starting Octave and communicating with it will be the
    lesser of my problems, in the end: I also need to share data
    structures, so as not to copy incessantly (otherwise better
    use sockets or Runtime.exec, which gives you the benefit of
    isolation as a bonus) and for that I'd need to understand
    Octave internals...

-- O.L.



-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by Michael Goffioul-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Jan 25, 2008 12:41 PM, Olivier Lefevre <lefevrol@...> wrote:
> Thanks. Two questions:
> 1) in a scenario where Java is in charge, where does something
>    like the existing java package fit in?

I'm not sure I understand what you mean. Could you be more
explicit?

> 2) I fear starting Octave and communicating with it will be the
>    lesser of my problems, in the end: I also need to share data
>    structures, so as not to copy incessantly (otherwise better
>    use sockets or Runtime.exec, which gives you the benefit of
>    isolation as a bonus) and for that I'd need to understand
>    Octave internals...

It depends on what you want to exchange. Making java code
to access octave data directly is not possible, because octave
C++ data is not part of the java-managed memory space. You
need to do some conversion; the java package provides the
functions box/unbox to convert the basic types (strings, scalars,
and even matrices [using the Matrix.java class]). Any octave
object that cannot be converted (struct, cell array) is stored
internally by the java package code, and given to JVM as
an OctaveReference object.

If you want to make the octave code able to access the Java
data, you can do this using the java package (through the
octave_java C++ class, which acts as a "reference to a java
object" that can be manipulated from octave).

Michael.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by xolotl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>> 1) in a scenario where Java is in charge, where does something
>>    like the existing java package fit in?
>
> I'm not sure I understand what you mean. Could you be more
> explicit?

You write: "[the javapackage] will be [initialized] automatically
by the octave engine when it needs it; [it] is able to attach itself
to a running JVM" etc, in other words you see a big role for the
java package but since it is for calling Java from Octave and I
want to do the opposite, I am confused.

In the past when I used JNI with the Java "in charge", no action
was required from the other side, which was completely passive.
But then the other side was a simple numerical library, not a
language interpreter.

> It depends on what you want to exchange. Making java code
> to access octave data directly is not possible, because octave
> C++ data is not part of the java-managed memory space.

I need to brush up my JNI: it's been a while, but I thought they'd
end up in the same process space, precisely.

-- O.L.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by Michael Goffioul-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Jan 25, 2008 1:23 PM, Olivier Lefevre <lefevrol@...> wrote:
> You write: "[the javapackage] will be [initialized] automatically
> by the octave engine when it needs it; [it] is able to attach itself
> to a running JVM" etc, in other words you see a big role for the
> java package but since it is for calling Java from Octave and I
> want to do the opposite, I am confused.

Well, the java package is the glue between octave and Java.
Up to now, it has only been used to do octave->java, but a
large part of it can be used to do java->octave as well
(the box/unbox conversion functions are the same, the
Matrix.java class...).

So let's say the java package contains everything you'd need:
the OctaveEngine class is part of octave.jar, while it's JNI
implementation is provided by __java__.oct. You have
octave installed along with the java package in the standard
locations. Then you can use the OctaveEngine class from
your Java app provided that:
1) you put octave.jar into the classpath
2) define the few system properties that are required to
make things work (mainly related to path locations): you
can see what's actually needed by looking at this example
http://visual-octave.svn.sourceforge.net/viewvc/visual-octave/trunk/console/virtualconsole/src/org/octave/ide/script/virtualconsole/octave/OctaveWrapper.java?revision=37&view=markup

> > It depends on what you want to exchange. Making java code
> > to access octave data directly is not possible, because octave
> > C++ data is not part of the java-managed memory space.
>
> I need to brush up my JNI: it's been a while, but I thought they'd
> end up in the same process space, precisely.

They are in the same process space, but not in the same
memory space. Let's say you have a array of doubles in C++
(double[] a). You can't make you java code access this array
directly. You must copy the double data into a java double[]
object, then it can be accessible from java code (the conversion
is done using JNI code).

Michael.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by xolotl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

OK, I'll give it a try.

-- O.L.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by xolotl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

By the way, won't all this pretty much require that Octave be
available in shared lib rather than executable form? Seeing
that octave.exe is just 5.5K and there are megabyte-sized
libraries under lib, I assume this is already the case but
what is the difference between octave.lib and octinterp.lib?
Which one do I need? And what is cruft.lib?

-- O.L.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java package

by Michael Goffioul-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 1/25/08, Olivier Lefevre <lefevrol@...> wrote:
> By the way, won't all this pretty much require that Octave be
> available in shared lib rather than executable form?

This is indeed already the case: octave.exe, octinterp.dll,
octave.dll and cruft.dll.

> Seeing
> that octave.exe is just 5.5K and there are megabyte-sized
> libraries under lib, I assume this is already the case but
> what is the difference between octave.lib and octinterp.lib?
> Which one do I need? And what is cruft.lib?

cruft.[lib|dll]: mainly FORTRAN code implementing low-level
computation routines

octave.[lib|dll]: octave C++ library (matrix classes, file system
operations...)

octinterp.[lib|dll]: octave interpreter library (defines the various
octave interpreted types, m-file parser, builtin functions...).

The file __java__.cc (from the java package) already links
against these 3 libraries (it's the default when you compile
with mkoctfile). So if you put your code into that file and
compile with mkoctfile, you don't need anything else.

Michael.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Java calling Octave via JNI

by MartinHepperle :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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