<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="de">
	<id>https://doc.expecco.de/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Javogel</id>
	<title>expecco Wiki (Version 25.x) - Benutzerbeiträge [de]</title>
	<link rel="self" type="application/atom+xml" href="https://doc.expecco.de/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Javogel"/>
	<link rel="alternate" type="text/html" href="https://doc.expecco.de/wiki/Spezial:Beitr%C3%A4ge/Javogel"/>
	<updated>2026-04-25T21:31:26Z</updated>
	<subtitle>Benutzerbeiträge</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://doc.expecco.de/index.php?title=DOT_NET_Interface_Library_v2&amp;diff=8426</id>
		<title>DOT NET Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=DOT_NET_Interface_Library_v2&amp;diff=8426"/>
		<updated>2017-03-15T13:28:27Z</updated>

		<summary type="html">&lt;p&gt;Javogel: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
&lt;br /&gt;
The .NET Interface Library (&amp;quot;.NET Bridge&amp;quot;) contains a mechanism to access Microsoft .NET (CLR) objects of a local or remote .NET application (a so-called .NET bridge), and an API for elementary blocks and a library of blocks to interact with these objects.&lt;br /&gt;
&lt;br /&gt;
== Programmatic Interface ==&lt;br /&gt;
&lt;br /&gt;
Access to .NET objects, classes and programs is done via a framework called &amp;quot;dotNET-Bridge&amp;quot;. This framework implements transparent forwarding of expecco messages (virtual function calls) from either Smalltalk or JavaScript code to .NET objects as existent in a local or remote .NET virtual machine. Also, return values, callBacks and exception information are passed back from the .NET program to expecco. A proxy-object mechanism which catches all function calls, wraps the arguments and sends a datagram to the other bridge side is used for this to be almost completely transparent to the Smalltalk/JavaScript code inside expecco.&lt;br /&gt;
&lt;br /&gt;
In addition to existing blocks of the .NET Interface Library, programmatic access to dotNET objects is sometimes useful or required. So the following information is useful if you want to write your own elementary .NET-blocks, or if you have to enhance the existing library by adding application-specific interface blocks.&lt;br /&gt;
&lt;br /&gt;
=== Initializing / Releasing the Bridge ===&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any dotNet object, the .NET side of the bridge has to be started, and a communication path to be setup.&lt;br /&gt;
All of the bridges classes are in the &amp;lt;CODE&amp;gt;DOTNET&amp;lt;/CODE&amp;gt; namespace; the main interface class is &amp;lt;CODE&amp;gt;DotNet&amp;lt;/CODE&amp;gt;, in this namespace:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle := DOTNET::DotNet newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle = DOTNET::DotNet.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the .NET-side of the bridge (the executable named &amp;quot;DotNetBridge.Server.exe&amp;quot;),&lt;br /&gt;
and waits for a connection request from this program.&lt;br /&gt;
&lt;br /&gt;
When finished, release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.close();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which shuts down the connection and terminates the executable.&lt;br /&gt;
&lt;br /&gt;
=== Loading Assemblies ===&lt;br /&gt;
Using the &amp;quot;loadLibrary&amp;quot;-function, wellknown assemblies can be loaded:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.loadLibrary(&amp;quot;System.Windows.Forms&amp;quot;);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.loadLibrary(&amp;quot;user32.dll&amp;quot;);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Arbitrary files which contain assemblies are loaded with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.loadFile(pathToDLL);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.loadExtension(pathToDLL);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
the former loads an assembly from the machine running the bridge server (DotNetBridge.Server.exe), the latter loads it from the machine running expecco.&lt;br /&gt;
&amp;lt;b&amp;gt;loadExtension&amp;lt;/b&amp;gt; also makes loaded assemblies available to dynamically compiled VB/C# compilation units.&lt;br /&gt;
&lt;br /&gt;
=== Accessing Globals ===&lt;br /&gt;
&lt;br /&gt;
Globals, nameSpaces and members of a namespace are accessed using message sends to the dotNet handle, where the message name is the name of the global, namespace or interface.&lt;br /&gt;
These messages can be chained, to access a hierarchy of namespaces.&lt;br /&gt;
For example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.System.Reflection.Assembly.LoadFile(&amp;quot;c:\foo\bar\myAssembly.dll&amp;quot;);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Instantiating a Class ===&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a global:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    b = dotNetHandle.Button.new();&lt;br /&gt;
    f = dotNetHandle.Form.new();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
once instanciated, any message can be sent transparently to such an .NET object, as if it was an expecco object:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    b.text(&amp;quot;Hello World&amp;quot;);&lt;br /&gt;
    f.controls.add(b);&lt;br /&gt;
    f.showDialog();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Callbacks ===&lt;br /&gt;
&lt;br /&gt;
Callbacks from .NET back into expecco are implemented via Smalltalk blocks (JavaScript anonymous functions).&lt;br /&gt;
The bridge automatically installs an appropriate callback, whenever a block/function is given to a .NET component as a callback or hook.&lt;br /&gt;
&lt;br /&gt;
Here is a complete example for creating a .NET form with a callback into expecco. This code could be put into an EB (Elementary Block) of an expecco activity diagram:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var dotNet, form, button;&lt;br /&gt;
&lt;br /&gt;
    // called when the .NET button is clicked&lt;br /&gt;
    function callBack() { &lt;br /&gt;
        dotNet.MessageBox.show(&amp;quot;Hello from expecco&amp;quot;); &lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    dotNet = DOTNET::DotNet.newWithServer();&lt;br /&gt;
    dotNet.loadLibrary(&amp;quot;System.Windows.Forms&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
    button = dotNet.Button.new();&lt;br /&gt;
    button.text(&amp;quot;Hello&amp;quot;);&lt;br /&gt;
    button.click.add( callBack );&lt;br /&gt;
        &lt;br /&gt;
    form = dotNet.Form.new();&lt;br /&gt;
    button.dock( dotNet.DockStyle.fill );&lt;br /&gt;
    form.controls.add( button );&lt;br /&gt;
        &lt;br /&gt;
    form.showDialog();&lt;br /&gt;
&lt;br /&gt;
    dotNet.close();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Remote Dynamic Code Injection ===&lt;br /&gt;
&lt;br /&gt;
C# and VB code can be dynamically loaded and compiled:&lt;br /&gt;
&lt;br /&gt;
C#:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    typeCode := &#039;&lt;br /&gt;
        using System;&lt;br /&gt;
&lt;br /&gt;
        namespace SomeNamespace&lt;br /&gt;
        {&lt;br /&gt;
            class SomeClass&lt;br /&gt;
            {&lt;br /&gt;
                public static void ConsoleWrite(string text)&lt;br /&gt;
                {&lt;br /&gt;
                    Console.Write(text)&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;.&lt;br /&gt;
    typeObject := dotnet getDotNetType:&#039;SomeNamespace.SomeClass&#039; fromCSharpSource:typeCode.&lt;br /&gt;
    typeObject ConsoleWrite:&#039;Hello, World!&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
VB:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    moduleCode := &#039;&lt;br /&gt;
        Imports System&lt;br /&gt;
&lt;br /&gt;
        Namespace SomeNamespace&lt;br /&gt;
            Public Module SomeModule&lt;br /&gt;
                Sub ConsoleWrite(ByVal text As String)&lt;br /&gt;
                    Console.WriteLine (text)&lt;br /&gt;
                End Sub&lt;br /&gt;
            End Module&lt;br /&gt;
        End Namespace&lt;br /&gt;
    &#039;.&lt;br /&gt;
    moduleObject := dotnet getDotNetType:&#039;SomeNamespace.SomeModule&#039; fromVBSource:moduleCode.&lt;br /&gt;
    moduleObject ConsoleWrite:&#039;Hello, World!&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Calling ActiveX/COM Objects===&lt;br /&gt;
&lt;br /&gt;
It&#039;s possible to make calls to ActiveX/COM Objects through the bridge.&lt;br /&gt;
&lt;br /&gt;
To do this, you need to create a dotNet Assembly with the types from a .tlb or .ocx file.&lt;br /&gt;
&lt;br /&gt;
This can be done with the tlbimp utility which is part of VisualStudio (reachable through the VS Dev CMD Shell...)&lt;br /&gt;
&lt;br /&gt;
In this shell, you can e.g. execute&lt;br /&gt;
&lt;br /&gt;
 tlbimp c:\windows\system32\wshom.ocx   &lt;br /&gt;
&lt;br /&gt;
which will create a file called IWshRuntimeLibrary.dll for the Windows Script Host Runtime Library in the current directory. This can then be used to automate the Windows Script Host Runtime Library Object.&lt;br /&gt;
&lt;br /&gt;
Here an example howto use this in code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  |dotNet shell buttonPressed|&lt;br /&gt;
&lt;br /&gt;
    dotNet := DotNet singletonInstance.&lt;br /&gt;
    dotNet loadFile:&#039;C:\Temp\IWshRuntimeLibrary.dll&#039;.&lt;br /&gt;
    shell := dotNet WshShellClass new.&lt;br /&gt;
    buttonPressed := shell popup:&#039;Hello from Micha&#039; secToWait:15 title:&#039;.NET Bridge Com Demo&#039; type:0.&lt;br /&gt;
    buttonPressed inspect                        &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
to be continued&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[Java Interface Library v2| Java Interface Plugin &amp;amp; Library]], which implements a likewise interface for Java applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&amp;lt;br&amp;gt;&lt;br /&gt;
Back to [[OnlineDocumentation#Library and Plugin Overview|Online Documentation]].&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=DOT_NET_Interface_Library_v2&amp;diff=8425</id>
		<title>DOT NET Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=DOT_NET_Interface_Library_v2&amp;diff=8425"/>
		<updated>2017-03-15T13:19:09Z</updated>

		<summary type="html">&lt;p&gt;Javogel: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
&lt;br /&gt;
The .NET Interface Library (&amp;quot;.NET Bridge&amp;quot;) contains a mechanism to access Microsoft .NET (CLR) objects of a local or remote .NET application (a so-called .NET bridge), and an API for elementary blocks and a library of blocks to interact with these objects.&lt;br /&gt;
&lt;br /&gt;
== Programmatic Interface ==&lt;br /&gt;
&lt;br /&gt;
Access to .NET objects, classes and programs is done via a framework called &amp;quot;dotNET-Bridge&amp;quot;. This framework implements transparent forwarding of expecco messages (virtual function calls) from either Smalltalk or JavaScript code to .NET objects as existent in a local or remote .NET virtual machine. Also, return values, callBacks and exception information are passed back from the .NET program to expecco. A proxy-object mechanism which catches all function calls, wraps the arguments and sends a datagram to the other bridge side is used for this to be almost completely transparent to the Smalltalk/JavaScript code inside expecco.&lt;br /&gt;
&lt;br /&gt;
In addition to existing blocks of the .NET Interface Library, programmatic access to dotNET objects is sometimes useful or required. So the following information is useful if you want to write your own elementary .NET-blocks, or if you have to enhance the existing library by adding application-specific interface blocks.&lt;br /&gt;
&lt;br /&gt;
=== Initializing / Releasing the Bridge ===&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any dotNet object, the .NET side of the bridge has to be started, and a communication path to be setup.&lt;br /&gt;
All of the bridges classes are in the &amp;lt;CODE&amp;gt;DOTNET&amp;lt;/CODE&amp;gt; namespace; the main interface class is &amp;lt;CODE&amp;gt;DotNet&amp;lt;/CODE&amp;gt;, in this namespace:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle := DOTNET::DotNet newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle = DOTNET::DotNet.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the .NET-side of the bridge (the executable named &amp;quot;DotNetBridge.Server.exe&amp;quot;),&lt;br /&gt;
and waits for a connection request from this program.&lt;br /&gt;
&lt;br /&gt;
When finished, release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.close();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which shuts down the connection and terminates the executable.&lt;br /&gt;
&lt;br /&gt;
=== Loading Assemblies ===&lt;br /&gt;
Using the &amp;quot;loadLibrary&amp;quot;-function, wellknown assemblies can be loaded:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.loadLibrary(&amp;quot;System.Windows.Forms&amp;quot;);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.loadLibrary(&amp;quot;user32.dll&amp;quot;);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Arbitrary files which contain assemblies are loaded with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.loadFile(pathToDLL);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.loadExtension(pathToDLL);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
the former loads an assembly from the machine running the bridge server (DotNetBridge.Server.exe), the latter loads it from the machine running expecco.&lt;br /&gt;
&lt;br /&gt;
=== Accessing Globals ===&lt;br /&gt;
&lt;br /&gt;
Globals, nameSpaces and members of a namespace are accessed using message sends to the dotNet handle, where the message name is the name of the global, namespace or interface.&lt;br /&gt;
These messages can be chained, to access a hierarchy of namespaces.&lt;br /&gt;
For example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.System.Reflection.Assembly.LoadFile(&amp;quot;c:\foo\bar\myAssembly.dll&amp;quot;);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Instantiating a Class ===&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a global:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    b = dotNetHandle.Button.new();&lt;br /&gt;
    f = dotNetHandle.Form.new();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
once instanciated, any message can be sent transparently to such an .NET object, as if it was an expecco object:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    b.text(&amp;quot;Hello World&amp;quot;);&lt;br /&gt;
    f.controls.add(b);&lt;br /&gt;
    f.showDialog();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Callbacks ===&lt;br /&gt;
&lt;br /&gt;
Callbacks from .NET back into expecco are implemented via Smalltalk blocks (JavaScript anonymous functions).&lt;br /&gt;
The bridge automatically installs an appropriate callback, whenever a block/function is given to a .NET component as a callback or hook.&lt;br /&gt;
&lt;br /&gt;
Here is a complete example for creating a .NET form with a callback into expecco. This code could be put into an EB (Elementary Block) of an expecco activity diagram:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var dotNet, form, button;&lt;br /&gt;
&lt;br /&gt;
    // called when the .NET button is clicked&lt;br /&gt;
    function callBack() { &lt;br /&gt;
        dotNet.MessageBox.show(&amp;quot;Hello from expecco&amp;quot;); &lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    dotNet = DOTNET::DotNet.newWithServer();&lt;br /&gt;
    dotNet.loadLibrary(&amp;quot;System.Windows.Forms&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
    button = dotNet.Button.new();&lt;br /&gt;
    button.text(&amp;quot;Hello&amp;quot;);&lt;br /&gt;
    button.click.add( callBack );&lt;br /&gt;
        &lt;br /&gt;
    form = dotNet.Form.new();&lt;br /&gt;
    button.dock( dotNet.DockStyle.fill );&lt;br /&gt;
    form.controls.add( button );&lt;br /&gt;
        &lt;br /&gt;
    form.showDialog();&lt;br /&gt;
&lt;br /&gt;
    dotNet.close();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Remote Dynamic Code Injection ===&lt;br /&gt;
&lt;br /&gt;
C# and VB code can be dynamically loaded and compiled:&lt;br /&gt;
&lt;br /&gt;
C#:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    typeCode := &#039;&lt;br /&gt;
        using System;&lt;br /&gt;
&lt;br /&gt;
        namespace SomeNamespace&lt;br /&gt;
        {&lt;br /&gt;
            class SomeClass&lt;br /&gt;
            {&lt;br /&gt;
                public static void ConsoleWrite(string text)&lt;br /&gt;
                {&lt;br /&gt;
                    Console.Write(text)&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;.&lt;br /&gt;
    typeObject := dotnet getDotNetType:&#039;SomeNamespace.SomeClass&#039; fromCSharpSource:typeCode.&lt;br /&gt;
    typeObject ConsoleWrite:&#039;Hello, World!&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
VB:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    moduleCode := &#039;&lt;br /&gt;
        Imports System&lt;br /&gt;
&lt;br /&gt;
        Namespace SomeNamespace&lt;br /&gt;
            Public Module SomeModule&lt;br /&gt;
                Sub ConsoleWrite(ByVal text As String)&lt;br /&gt;
                    Console.WriteLine (text)&lt;br /&gt;
                End Sub&lt;br /&gt;
            End Module&lt;br /&gt;
        End Namespace&lt;br /&gt;
    &#039;.&lt;br /&gt;
    moduleObject := dotnet getDotNetType:&#039;SomeNamespace.SomeModule&#039; fromVBSource:moduleCode.&lt;br /&gt;
    moduleObject ConsoleWrite:&#039;Hello, World!&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Calling ActiveX/COM Objects===&lt;br /&gt;
&lt;br /&gt;
It&#039;s possible to make calls to ActiveX/COM Objects through the bridge.&lt;br /&gt;
&lt;br /&gt;
To do this, you need to create a dotNet Assembly with the types from a .tlb or .ocx file.&lt;br /&gt;
&lt;br /&gt;
This can be done with the tlbimp utility which is part of VisualStudio (reachable through the VS Dev CMD Shell...)&lt;br /&gt;
&lt;br /&gt;
In this shell, you can e.g. execute&lt;br /&gt;
&lt;br /&gt;
 tlbimp c:\windows\system32\wshom.ocx   &lt;br /&gt;
&lt;br /&gt;
which will create a file called IWshRuntimeLibrary.dll for the Windows Script Host Runtime Library in the current directory. This can then be used to automate the Windows Script Host Runtime Library Object.&lt;br /&gt;
&lt;br /&gt;
Here an example howto use this in code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  |dotNet shell buttonPressed|&lt;br /&gt;
&lt;br /&gt;
    dotNet := DotNet singletonInstance.&lt;br /&gt;
    dotNet loadFile:&#039;C:\Temp\IWshRuntimeLibrary.dll&#039;.&lt;br /&gt;
    shell := dotNet WshShellClass new.&lt;br /&gt;
    buttonPressed := shell popup:&#039;Hello from Micha&#039; secToWait:15 title:&#039;.NET Bridge Com Demo&#039; type:0.&lt;br /&gt;
    buttonPressed inspect                        &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
to be continued&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[Java Interface Library v2| Java Interface Plugin &amp;amp; Library]], which implements a likewise interface for Java applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&amp;lt;br&amp;gt;&lt;br /&gt;
Back to [[OnlineDocumentation#Library and Plugin Overview|Online Documentation]].&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=DOT_NET_Interface_Library_v2&amp;diff=8424</id>
		<title>DOT NET Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=DOT_NET_Interface_Library_v2&amp;diff=8424"/>
		<updated>2017-03-15T13:18:18Z</updated>

		<summary type="html">&lt;p&gt;Javogel: Die Seite wurde neu angelegt: „== Overview ==  The .NET Interface Library (&amp;quot;.NET Bridge&amp;quot;) contains a mechanism to access Microsoft .NET (CLR) objects of a local or remote .NET application (a…“&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
&lt;br /&gt;
The .NET Interface Library (&amp;quot;.NET Bridge&amp;quot;) contains a mechanism to access Microsoft .NET (CLR) objects of a local or remote .NET application (a so-called .NET bridge), and an API for elementary blocks and a library of blocks to interact with these objects.&lt;br /&gt;
&lt;br /&gt;
== Programmatic Interface ==&lt;br /&gt;
&lt;br /&gt;
Access to .NET objects, classes and programs is done via a framework called &amp;quot;dotNET-Bridge&amp;quot;. This framework implements transparent forwarding of expecco messages (virtual function calls) from either Smalltalk or JavaScript code to .NET objects as existent in a local or remote .NET virtual machine. Also, return values, callBacks and exception information are passed back from the .NET program to expecco. A proxy-object mechanism which catches all function calls, wraps the arguments and sends a datagram to the other bridge side is used for this to be almost completely transparent to the Smalltalk/JavaScript code inside expecco.&lt;br /&gt;
&lt;br /&gt;
In addition to existing blocks of the .NET Interface Library, programmatic access to dotNET objects is sometimes useful or required. So the following information is useful if you want to write your own elementary .NET-blocks, or if you have to enhance the existing library by adding application-specific interface blocks.&lt;br /&gt;
&lt;br /&gt;
=== Initializing / Releasing the Bridge ===&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any dotNet object, the .NET side of the bridge has to be started, and a communication path to be setup.&lt;br /&gt;
All of the bridges classes are in the &amp;lt;CODE&amp;gt;DOTNET&amp;lt;/CODE&amp;gt; namespace; the main interface class is &amp;lt;CODE&amp;gt;DotNet&amp;lt;/CODE&amp;gt;, in this namespace:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle := DOTNET::DotNet newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle = DOTNET::DotNet.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the .NET-side of the bridge (the executable named &amp;quot;DotNetBridge.Server.exe&amp;quot;),&lt;br /&gt;
and waits for a connection request from this program.&lt;br /&gt;
&lt;br /&gt;
When finished, release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.close();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which shuts down the connection and terminates the executable.&lt;br /&gt;
&lt;br /&gt;
=== Loading Assemblies ===&lt;br /&gt;
Using the &amp;quot;loadLibrary&amp;quot;-function, wellknown assemblies can be loaded:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.loadLibrary(&amp;quot;System.Windows.Forms&amp;quot;);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.loadLibrary(&amp;quot;user32.dll&amp;quot;);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Arbitrary files which contain assemblies are loaded with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.loadFile(pathToDLL);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.loadExtension(pathToDLL);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
the former loads an assembly from the machine running the bridge server (DotNetBridge.Server.exe), the latter loads it from the machine running expecco.&lt;br /&gt;
&lt;br /&gt;
=== Accessing Globals ===&lt;br /&gt;
&lt;br /&gt;
Globals, nameSpaces and members of a namespace are accessed using message sends to the dotNet handle, where the message name is the name of the global, namespace or interface.&lt;br /&gt;
These messages can be chained, to access a hierarchy of namespaces.&lt;br /&gt;
For example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    dotNetHandle.System.Reflection.Assembly.LoadFile(&amp;quot;c:\foo\bar\myAssembly.dll&amp;quot;);&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Instantiating a Class ===&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a global:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    b = dotNetHandle.Button.new();&lt;br /&gt;
    f = dotNetHandle.Form.new();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
once instanciated, any message can be sent transparently to such an .NET object, as if it was an expecco object:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    b.text(&amp;quot;Hello World&amp;quot;);&lt;br /&gt;
    f.controls.add(b);&lt;br /&gt;
    f.showDialog();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Callbacks ===&lt;br /&gt;
&lt;br /&gt;
Callbacks from .NET back into expecco are implemented via Smalltalk blocks (JavaScript anonymous functions).&lt;br /&gt;
The bridge automatically installs an appropriate callback, whenever a block/function is given to a .NET component as a callback or hook.&lt;br /&gt;
&lt;br /&gt;
Here is a complete example for creating a .NET form with a callback into expecco. This code could be put into an EB (Elementary Block) of an expecco activity diagram:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var dotNet, form, button;&lt;br /&gt;
&lt;br /&gt;
    // called when the .NET button is clicked&lt;br /&gt;
    function callBack() { &lt;br /&gt;
        dotNet.MessageBox.show(&amp;quot;Hello from expecco&amp;quot;); &lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    dotNet = DOTNET::DotNet.newWithServer();&lt;br /&gt;
    dotNet.loadLibrary(&amp;quot;System.Windows.Forms&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
    button = dotNet.Button.new();&lt;br /&gt;
    button.text(&amp;quot;Hello&amp;quot;);&lt;br /&gt;
    button.click.add( callBack );&lt;br /&gt;
        &lt;br /&gt;
    form = dotNet.Form.new();&lt;br /&gt;
    button.dock( dotNet.DockStyle.fill );&lt;br /&gt;
    form.controls.add( button );&lt;br /&gt;
        &lt;br /&gt;
    form.showDialog();&lt;br /&gt;
&lt;br /&gt;
    dotNet.close();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Remote Dynamic Code Injection ===&lt;br /&gt;
&lt;br /&gt;
C# and VB code can be dynamically loaded and compiled:&lt;br /&gt;
&lt;br /&gt;
C#:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    typeCode := &#039;&lt;br /&gt;
        using System;&lt;br /&gt;
&lt;br /&gt;
        namespace SomeNamespace&lt;br /&gt;
        {&lt;br /&gt;
            class SomeClass&lt;br /&gt;
            {&lt;br /&gt;
                public static void ConsoleWrite(string text)&lt;br /&gt;
                {&lt;br /&gt;
                    Console.Write(text)&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;.&lt;br /&gt;
    typeObject := dotnet getDotNetType:&#039;SomeNamespace.SomeClass&#039; fromCSharpSource:typeCode.&lt;br /&gt;
    typeObject ConsoleWrite:&#039;Hello, World!&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
VB:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    moduleCode := &#039;&lt;br /&gt;
        Imports System&lt;br /&gt;
&lt;br /&gt;
        Namespace SomeNamespace&lt;br /&gt;
            Public Module SomeModule&lt;br /&gt;
                Sub ConsoleWrite(ByVal text As String)&lt;br /&gt;
                    Console.WriteLine (&amp;quot;Hello World using Visual Basic!&amp;quot;)&lt;br /&gt;
                End Sub&lt;br /&gt;
            End Module&lt;br /&gt;
        End Namespace&lt;br /&gt;
    &#039;.&lt;br /&gt;
    moduleObject := dotnet getDotNetType:&#039;SomeNamespace.SomeModule&#039; fromVBSource:moduleCode.&lt;br /&gt;
    moduleObject ConsoleWrite:&#039;Hello, World!&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Calling ActiveX/COM Objects===&lt;br /&gt;
&lt;br /&gt;
It&#039;s possible to make calls to ActiveX/COM Objects through the bridge.&lt;br /&gt;
&lt;br /&gt;
To do this, you need to create a dotNet Assembly with the types from a .tlb or .ocx file.&lt;br /&gt;
&lt;br /&gt;
This can be done with the tlbimp utility which is part of VisualStudio (reachable through the VS Dev CMD Shell...)&lt;br /&gt;
&lt;br /&gt;
In this shell, you can e.g. execute&lt;br /&gt;
&lt;br /&gt;
 tlbimp c:\windows\system32\wshom.ocx   &lt;br /&gt;
&lt;br /&gt;
which will create a file called IWshRuntimeLibrary.dll for the Windows Script Host Runtime Library in the current directory. This can then be used to automate the Windows Script Host Runtime Library Object.&lt;br /&gt;
&lt;br /&gt;
Here an example howto use this in code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  |dotNet shell buttonPressed|&lt;br /&gt;
&lt;br /&gt;
    dotNet := DotNet singletonInstance.&lt;br /&gt;
    dotNet loadFile:&#039;C:\Temp\IWshRuntimeLibrary.dll&#039;.&lt;br /&gt;
    shell := dotNet WshShellClass new.&lt;br /&gt;
    buttonPressed := shell popup:&#039;Hello from Micha&#039; secToWait:15 title:&#039;.NET Bridge Com Demo&#039; type:0.&lt;br /&gt;
    buttonPressed inspect                        &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
to be continued&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[Java Interface Library v2| Java Interface Plugin &amp;amp; Library]], which implements a likewise interface for Java applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&amp;lt;br&amp;gt;&lt;br /&gt;
Back to [[OnlineDocumentation#Library and Plugin Overview|Online Documentation]].&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6241</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6241"/>
		<updated>2016-06-01T12:39:04Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* boxedDo:aBlock */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &#039;&#039;Extras&#039;&#039; ► &#039;&#039;Settings&#039;&#039; ► &#039;&#039;Plugins&#039;&#039; ► &#039;&#039;Java Bridge&#039;&#039; and tick the &#039;&#039;Use new Bridge (experimental)&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltalk side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Boxed Primitives ==&lt;br /&gt;
&lt;br /&gt;
In version 1 of the java bridge boxed primitives were unboxed automatically and the identity was not preserved.&lt;br /&gt;
In version 2 a mechanism is provided allowing to preserve the identity of boxed primitives: [[ #boxedDo:aBlock | boxedDo:aBlock ]].&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java boxedDo:[&lt;br /&gt;
        boxedInt := java getJavaClass:&#039;java.lang.Integer&#039; new:30. &amp;quot;/ instantiate a new boxed integer. boxedInt now holds a reference to this object&lt;br /&gt;
    ].&lt;br /&gt;
    intValue := boxedInt value. &amp;quot;/ extract the value of the boxed integer. (no overhead)&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Asynchronous Communication ==&lt;br /&gt;
&lt;br /&gt;
In the new Bridge, everything can be done asynchronously. This is achieved via the &amp;lt;CODE&amp;gt;asyncDo:aBlock&amp;lt;/CODE&amp;gt; method. &lt;br /&gt;
Every request sent to the Bridge inside of aBlock will immediately return a lazy value.&lt;br /&gt;
This lazy value will block upon access until the actual result is received from the java side.&lt;br /&gt;
The order of execution is guaranteed to be the same on the java side as on the smalltalk side.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java asyncDo:[&lt;br /&gt;
        lazyInteger := (java getJavaClass:&#039;java.lang.Integer&#039;) new:40. &amp;quot;/ this will return immediately&lt;br /&gt;
    ].&lt;br /&gt;
    actualInteger := lazyInteger value. &amp;quot;/ this will block until the result is available&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := (java getJavaClass:&#039;javax.swing.JButton&#039;) new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := (java getJavaClass:&#039;java.awt.event.MouseListener&#039;) new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can now be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Simplified instantiation and access of arrays. (see [[#Arrays | Arrays]])&lt;br /&gt;
* Every request to the java side can now be done asynchronously. (see [[#Asynchronous Communication | Asynchronous Communication]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any boxed values in aBlock returned from the JVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Java Interface Library | Version 1 of the Java Interface Library]]&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6240</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6240"/>
		<updated>2016-06-01T12:38:06Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Boxed Primitives */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &#039;&#039;Extras&#039;&#039; ► &#039;&#039;Settings&#039;&#039; ► &#039;&#039;Plugins&#039;&#039; ► &#039;&#039;Java Bridge&#039;&#039; and tick the &#039;&#039;Use new Bridge (experimental)&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltalk side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Boxed Primitives ==&lt;br /&gt;
&lt;br /&gt;
In version 1 of the java bridge boxed primitives were unboxed automatically and the identity was not preserved.&lt;br /&gt;
In version 2 a mechanism is provided allowing to preserve the identity of boxed primitives: [[ #boxedDo:aBlock | boxedDo:aBlock ]].&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java boxedDo:[&lt;br /&gt;
        boxedInt := java getJavaClass:&#039;java.lang.Integer&#039; new:30. &amp;quot;/ instantiate a new boxed integer. boxedInt now holds a reference to this object&lt;br /&gt;
    ].&lt;br /&gt;
    intValue := boxedInt value. &amp;quot;/ extract the value of the boxed integer. (no overhead)&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Asynchronous Communication ==&lt;br /&gt;
&lt;br /&gt;
In the new Bridge, everything can be done asynchronously. This is achieved via the &amp;lt;CODE&amp;gt;asyncDo:aBlock&amp;lt;/CODE&amp;gt; method. &lt;br /&gt;
Every request sent to the Bridge inside of aBlock will immediately return a lazy value.&lt;br /&gt;
This lazy value will block upon access until the actual result is received from the java side.&lt;br /&gt;
The order of execution is guaranteed to be the same on the java side as on the smalltalk side.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java asyncDo:[&lt;br /&gt;
        lazyInteger := (java getJavaClass:&#039;java.lang.Integer&#039;) new:40. &amp;quot;/ this will return immediately&lt;br /&gt;
    ].&lt;br /&gt;
    actualInteger := lazyInteger value. &amp;quot;/ this will block until the result is available&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := (java getJavaClass:&#039;javax.swing.JButton&#039;) new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := (java getJavaClass:&#039;java.awt.event.MouseListener&#039;) new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can now be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Simplified instantiation and access of arrays. (see [[#Arrays | Arrays]])&lt;br /&gt;
* Every request to the java side can now be done asynchronously. (see [[#Asynchronous Communication | Asynchronous Communication]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Java Interface Library | Version 1 of the Java Interface Library]]&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6239</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6239"/>
		<updated>2016-06-01T12:37:37Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Boxed Primitives */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &#039;&#039;Extras&#039;&#039; ► &#039;&#039;Settings&#039;&#039; ► &#039;&#039;Plugins&#039;&#039; ► &#039;&#039;Java Bridge&#039;&#039; and tick the &#039;&#039;Use new Bridge (experimental)&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltalk side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Boxed Primitives ==&lt;br /&gt;
&lt;br /&gt;
In version 1 of the java bridge boxed primitives were unboxed automatically and the identity was not preserved.&lt;br /&gt;
In version 2 a mechanism is provided allowing to preserve the identity of boxed primitives: [[ #boxedDo:aBlock | boxedDo:aBlock ]].&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java boxedDo:[&lt;br /&gt;
        boxedInt := java getJavaClass:&#039;java.lang.Integer&#039; new:30. &amp;quot;/ instantiate a new boxed integer. boxedInt now holds a reference to this object&lt;br /&gt;
    ].&lt;br /&gt;
    intValue := boxedInt value. &amp;quot;/ extract the value of the boxed integer. (does not require a roundtrip)&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Asynchronous Communication ==&lt;br /&gt;
&lt;br /&gt;
In the new Bridge, everything can be done asynchronously. This is achieved via the &amp;lt;CODE&amp;gt;asyncDo:aBlock&amp;lt;/CODE&amp;gt; method. &lt;br /&gt;
Every request sent to the Bridge inside of aBlock will immediately return a lazy value.&lt;br /&gt;
This lazy value will block upon access until the actual result is received from the java side.&lt;br /&gt;
The order of execution is guaranteed to be the same on the java side as on the smalltalk side.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java asyncDo:[&lt;br /&gt;
        lazyInteger := (java getJavaClass:&#039;java.lang.Integer&#039;) new:40. &amp;quot;/ this will return immediately&lt;br /&gt;
    ].&lt;br /&gt;
    actualInteger := lazyInteger value. &amp;quot;/ this will block until the result is available&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := (java getJavaClass:&#039;javax.swing.JButton&#039;) new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := (java getJavaClass:&#039;java.awt.event.MouseListener&#039;) new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can now be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Simplified instantiation and access of arrays. (see [[#Arrays | Arrays]])&lt;br /&gt;
* Every request to the java side can now be done asynchronously. (see [[#Asynchronous Communication | Asynchronous Communication]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Java Interface Library | Version 1 of the Java Interface Library]]&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6238</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6238"/>
		<updated>2016-06-01T12:36:52Z</updated>

		<summary type="html">&lt;p&gt;Javogel: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &#039;&#039;Extras&#039;&#039; ► &#039;&#039;Settings&#039;&#039; ► &#039;&#039;Plugins&#039;&#039; ► &#039;&#039;Java Bridge&#039;&#039; and tick the &#039;&#039;Use new Bridge (experimental)&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltalk side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Boxed Primitives ==&lt;br /&gt;
&lt;br /&gt;
In version 1 of the java bridge boxed primitives were unboxed automatically and the identity was not preserved.&lt;br /&gt;
In version 2 a mechanism is provided allowing to preserve the identity of boxed primitives: [[ #boxedDo:aBlock | boxedDo:aBlock ]].&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java boxedDo:[&lt;br /&gt;
        boxedInt := java getJavaClass:&#039;java.lang.Integer&#039; new:30. &amp;quot;/ instantiate a new boxed integer. boxedInt now holds a reference to this integer and not the value&lt;br /&gt;
    ].&lt;br /&gt;
    intValue := boxedInt value. &amp;quot;/ extract the value of the boxed integer. (does not require a roundtrip)&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Asynchronous Communication ==&lt;br /&gt;
&lt;br /&gt;
In the new Bridge, everything can be done asynchronously. This is achieved via the &amp;lt;CODE&amp;gt;asyncDo:aBlock&amp;lt;/CODE&amp;gt; method. &lt;br /&gt;
Every request sent to the Bridge inside of aBlock will immediately return a lazy value.&lt;br /&gt;
This lazy value will block upon access until the actual result is received from the java side.&lt;br /&gt;
The order of execution is guaranteed to be the same on the java side as on the smalltalk side.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java asyncDo:[&lt;br /&gt;
        lazyInteger := (java getJavaClass:&#039;java.lang.Integer&#039;) new:40. &amp;quot;/ this will return immediately&lt;br /&gt;
    ].&lt;br /&gt;
    actualInteger := lazyInteger value. &amp;quot;/ this will block until the result is available&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := (java getJavaClass:&#039;javax.swing.JButton&#039;) new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := (java getJavaClass:&#039;java.awt.event.MouseListener&#039;) new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can now be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Simplified instantiation and access of arrays. (see [[#Arrays | Arrays]])&lt;br /&gt;
* Every request to the java side can now be done asynchronously. (see [[#Asynchronous Communication | Asynchronous Communication]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Java Interface Library | Version 1 of the Java Interface Library]]&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6237</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6237"/>
		<updated>2016-06-01T12:19:09Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Activating the new Java Bridge */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &#039;&#039;Extras&#039;&#039; ► &#039;&#039;Settings&#039;&#039; ► &#039;&#039;Plugins&#039;&#039; ► &#039;&#039;Java Bridge&#039;&#039; and tick the &#039;&#039;Use new Bridge (experimental)&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltalk side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Asynchronous Communication ==&lt;br /&gt;
&lt;br /&gt;
In the new Bridge, everything can be done asynchronously. This is achieved via the &amp;lt;CODE&amp;gt;asyncDo:aBlock&amp;lt;/CODE&amp;gt; method. &lt;br /&gt;
Every request sent to the Bridge inside of aBlock will immediately return a lazy value.&lt;br /&gt;
This lazy value will block upon access until the actual result is received from the java side.&lt;br /&gt;
The order of execution is guaranteed to be the same on the java side as on the smalltalk side.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java asyncDo:[&lt;br /&gt;
        lazyInteger := (java getJavaClass:&#039;java.lang.Integer&#039;) new:40. &amp;quot;/ this will return immediately&lt;br /&gt;
    ].&lt;br /&gt;
    actualInteger := lazyInteger value. &amp;quot;/ this will block until the result is available&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := (java getJavaClass:&#039;javax.swing.JButton&#039;) new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := (java getJavaClass:&#039;java.awt.event.MouseListener&#039;) new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can now be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Simplified instantiation and access of arrays. (see [[#Arrays | Arrays]])&lt;br /&gt;
* Every request to the java side can now be done asynchronously. (see [[#Asynchronous Communication | Asynchronous Communication]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Java Interface Library | Version 1 of the Java Interface Library]]&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6236</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6236"/>
		<updated>2016-06-01T11:39:15Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltalk side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Asynchronous Communication ==&lt;br /&gt;
&lt;br /&gt;
In the new Bridge, everything can be done asynchronously. This is achieved via the &amp;lt;CODE&amp;gt;asyncDo:aBlock&amp;lt;/CODE&amp;gt; method. &lt;br /&gt;
Every request sent to the Bridge inside of aBlock will immediately return a lazy value.&lt;br /&gt;
This lazy value will block upon access until the actual result is received from the java side.&lt;br /&gt;
The order of execution is guaranteed to be the same on the java side as on the smalltalk side.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java asyncDo:[&lt;br /&gt;
        lazyInteger := (java getJavaClass:&#039;java.lang.Integer&#039;) new:40. &amp;quot;/ this will return immediately&lt;br /&gt;
    ].&lt;br /&gt;
    actualInteger := lazyInteger value. &amp;quot;/ this will block until the result is available&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := (java getJavaClass:&#039;javax.swing.JButton&#039;) new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := (java getJavaClass:&#039;java.awt.event.MouseListener&#039;) new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can now be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Simplified instantiation and access of arrays. (see [[#Arrays | Arrays]])&lt;br /&gt;
* Every request to the java side can now be done asynchronously. (see [[#Asynchronous Communication | Asynchronous Communication]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Java Interface Library | Version 1 of the Java Interface Library]]&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6235</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6235"/>
		<updated>2016-06-01T11:05:44Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* General */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltalk side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Asynchronous Communication ==&lt;br /&gt;
&lt;br /&gt;
In the new Bridge, everything can be done asynchronously. This is achieved via the &amp;lt;CODE&amp;gt;asyncDo:aBlock&amp;lt;/CODE&amp;gt; method. &lt;br /&gt;
Every request sent to the Bridge inside of aBlock will immediately return a lazy value.&lt;br /&gt;
This lazy value will block upon access until the actual result is received from the java side.&lt;br /&gt;
The order of execution is guaranteed to be the same on the java side as on the smalltalk side.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java asyncDo:[&lt;br /&gt;
        lazyInteger := (java getJavaClass:&#039;java.lang.Integer&#039;) new:40. &amp;quot;/ this will return immediately&lt;br /&gt;
    ].&lt;br /&gt;
    actualInteger := lazyInteger value. &amp;quot;/ this will block until the result is available&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can now be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Simplified instantiation and access of arrays. (see [[#Arrays | Arrays]])&lt;br /&gt;
* Every request to the java side can now be done asynchronously. (see [[#Asynchronous Communication | Asynchronous Communication]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Java Interface Library | Version 1 of the Java Interface Library]]&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6234</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6234"/>
		<updated>2016-06-01T11:00:23Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Asynchronous Communication */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltalk side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Asynchronous Communication ==&lt;br /&gt;
&lt;br /&gt;
In the new Bridge, everything can be done asynchronously. This is achieved via the &amp;lt;CODE&amp;gt;asyncDo:aBlock&amp;lt;/CODE&amp;gt; method. &lt;br /&gt;
Every request sent to the Bridge inside of aBlock will immediately return a lazy value.&lt;br /&gt;
This lazy value will block upon access until the actual result is received from the java side.&lt;br /&gt;
The order of execution is guaranteed to be the same on the java side as on the smalltalk side.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java asyncDo:[&lt;br /&gt;
        lazyInteger := (java getJavaClass:&#039;java.lang.Integer&#039;) new:40. &amp;quot;/ this will return immediately&lt;br /&gt;
    ].&lt;br /&gt;
    actualInteger := lazyInteger value. &amp;quot;/ this will block until the result is available&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can now be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized. (see [[#Arrays | Arrays]])&lt;br /&gt;
* Every request to the java side can now be done asynchronously. (see [[#Asynchronous Communication | Asynchronous Communication]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Java Interface Library | Version 1 of the Java Interface Library]]&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library/en&amp;diff=6233</id>
		<title>Java Interface Library/en</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library/en&amp;diff=6233"/>
		<updated>2016-06-01T10:46:19Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Documentation for expecco 2.1&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
In addition to what existing blocks of the Java Interface Library provide, programmatic access to Java objects is sometimes required to perform special functions. The following information is useful if you want to write your own elementary Java-blocks, or if you have to enhance the existing library by adding application-specific interface blocks.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\technologyBridge\javaBridge\javaBridge_Server_Client\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connection Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the class path:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java addJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
be aware, that if the JVM is on a remote machine, you should ensure somehow that the jar file is present on that machine. If required, transfer it to the machine&#039;s &amp;quot;tmp&amp;quot; directory, and use that folder as path.&lt;br /&gt;
(you can use another expecco block for that file transfer, or write a little batch/shell script to do that).&lt;br /&gt;
&lt;br /&gt;
== Java Package Import and Class Access==&lt;br /&gt;
&lt;br /&gt;
The import (loading) of additional required packages is done automatically in the JVM, when one of the package&#039;s classes is accessed for the very first time. In other words, when the Java code references a class for the first time, whose package is not yet loaded, it will search for the package along the class path and load it. Thus the above &amp;quot;addToClassPath:&amp;quot; actually does not yet verify or load the package, but only prepare any future &amp;quot;load on demand&amp;quot;. This may be a bit dangerous, as failures may be reported much later, when a missing class is actually loaded (so better double check, that all the class pathes are valid, and the corresponding files are present on the system on which the JVM runs). &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This following code sample simple references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;. Note that in Smalltalk code, the dots are replaced by underscores and the full class name is written as a message send to the java bridge handle:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java javax_swing_JFrame. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After that call, the JavaVM has loaded the required package(s) and now knows the package &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt;. It will lookup short form class names in the set of known packages. Now we can access all classes of that package without a need to provide the full class name. For example, if we now want to access the class &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JButton&amp;quot;&amp;lt;/CODE&amp;gt;, it can be done via the class&#039;s short name:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java JButton. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;ATTENTION:&#039;&#039;&#039; If a classes&#039; short form exists in more than one package on the Java side, then you still have to specify the full name of the class. Otherwise, the first matching class will be taken and that&#039;s probably not the one you were looking for!&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java javax_swing_JFrame new.&lt;br /&gt;
    button := java JButton new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance (and as a side effect of referring to the class will import the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package). The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java javax_swing_JFrame isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or if the package is already known:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java JFrame isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java javax_swing_JFrame EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (using short names):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java JFrame EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener mousePressed:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; tothe Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaininf java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer update:[:sourceObservable :argument| &lt;br /&gt;
        argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
        ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
    ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    java RemoteClassInject injectClassCode:&#039;myPackage.test&#039; className:&#039;MyInjectedClass &#039; classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    java myPackage_test_MyInjectedClass new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java RemoteClassInject injectClassFile:packageName className:className classCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java javax_swing_JFrame new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
    listener mousePressed:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener mouseReleased:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener mouseExited:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====addJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the class path of the running JavaVM. If the jar depends on other jars or librarys you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====addLibraryByPath:aPath=====&lt;br /&gt;
* Adds a library to the library path of the running JavaVM. If the library depends on other librarys you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the library file as String.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&amp;lt;br&amp;gt;&lt;br /&gt;
[[Java Interface Library v2 | Version 2 of the Java Interface Library]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&amp;lt;br&amp;gt;&lt;br /&gt;
Back to [[OnlineDocumentation#Library and Plugin Overview|Online Documentation]].&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6232</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6232"/>
		<updated>2016-06-01T10:45:06Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* General */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltalk side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Asynchronous Communication ==&lt;br /&gt;
&lt;br /&gt;
In the new Bridge, everything can be done asynchronously. This is achieved via the &amp;lt;CODE&amp;gt;asyncDo:aBlock&amp;lt;/CODE&amp;gt; method. &lt;br /&gt;
Every request sent to the Bridge inside of aBlock will immediately return a lazy value.&lt;br /&gt;
This lazy value will block upon access until the actual result is received from the java side.&lt;br /&gt;
The order of execution is guaranteed to be the same on the java side as on the smalltalk side.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java asyncDo:[&lt;br /&gt;
        lazyInteger := (java getJavaClass:&#039;java.lang.Integer&#039;) new:40. &amp;quot;/ this will return immediately&lt;br /&gt;
    ].&lt;br /&gt;
    actualInteger := lazyInteger value. &amp;quot;/ this will block until the result of the previous call is available&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can now be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized. (see [[#Arrays | Arrays]])&lt;br /&gt;
* Every request to the java side can now be done asynchronously. (see [[#Asynchronous Communication | Asynchronous Communication]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Java Interface Library | Version 1 of the Java Interface Library]]&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6230</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6230"/>
		<updated>2016-06-01T10:43:32Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltalk side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Asynchronous Communication ==&lt;br /&gt;
&lt;br /&gt;
In the new Bridge, everything can be done asynchronously. This is achieved via the &amp;lt;CODE&amp;gt;asyncDo:aBlock&amp;lt;/CODE&amp;gt; method. &lt;br /&gt;
Every request sent to the Bridge inside of aBlock will immediately return a lazy value.&lt;br /&gt;
This lazy value will block upon access until the actual result is received from the java side.&lt;br /&gt;
The order of execution is guaranteed to be the same on the java side as on the smalltalk side.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java asyncDo:[&lt;br /&gt;
        lazyInteger := (java getJavaClass:&#039;java.lang.Integer&#039;) new:40. &amp;quot;/ this will return immediately&lt;br /&gt;
    ].&lt;br /&gt;
    actualInteger := lazyInteger value. &amp;quot;/ this will block until the result of the previous call is available&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can now be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized. (see [[#Arrays | Arrays]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Java Interface Library | Version 1 of the Java Interface Library]]&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6229</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6229"/>
		<updated>2016-06-01T10:36:26Z</updated>

		<summary type="html">&lt;p&gt;Javogel: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltalk side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Asynchronous Communication ==&lt;br /&gt;
&lt;br /&gt;
In the new Bridge, everything can be done asynchronously. This is achieved via the &amp;lt;CODE&amp;gt;asyncDo:aBlock&amp;lt;/CODE&amp;gt; method. &lt;br /&gt;
Every request sent to the Bridge inside of aBlock will immediately return a lazy value.&lt;br /&gt;
This lazy value will block upon access until the actual result is received from the java side.&lt;br /&gt;
The order of execution is guaranteed to be the same on the java side as on the smalltalk side.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java asyncDo:[&lt;br /&gt;
        lazyInteger := (java getJavaClass:&#039;java.lang.Integer&#039;) new:40. &amp;quot;/ this will return immediately&lt;br /&gt;
    ].&lt;br /&gt;
    actualInteger := lazyInteger value. &amp;quot;/ this will block until the result of the previous call is available&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can now be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized. (see [[#Arrays | Arrays]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6228</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6228"/>
		<updated>2016-06-01T09:37:40Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* General */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltalk side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can now be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized. (see [[#Arrays | Arrays]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6227</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6227"/>
		<updated>2016-06-01T09:26:39Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Passing Smalltalk Collections to Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltalk side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized. (see [[#Arrays | Arrays]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6226</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6226"/>
		<updated>2016-06-01T09:23:30Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Accessing Arrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltal side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;short[]&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized. (see [[#Arrays | Arrays]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6225</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6225"/>
		<updated>2016-06-01T09:23:05Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Instantiating Arrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltal side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;int[]&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;[S&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized. (see [[#Arrays | Arrays]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6224</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6224"/>
		<updated>2016-06-01T09:19:39Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Instantiating Arrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltal side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;[I&#039;) new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := (java getJavaClass:&#039;[I&#039;) new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;[S&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized. (see [[#Arrays | Arrays]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6223</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6223"/>
		<updated>2016-06-01T09:19:12Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Accessing Arrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltal side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := java getJavaClass:&#039;[I&#039; new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := java getJavaClass:&#039;[I&#039; new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := (java getJavaClass:&#039;[S&#039;) new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized. (see [[#Arrays | Arrays]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6222</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6222"/>
		<updated>2016-06-01T09:07:13Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Accessing Arrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltal side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := java getJavaClass:&#039;[I&#039; new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := java getJavaClass:&#039;[I&#039; new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := java getJavaClass:&#039;[S&#039; new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    (java getJavaClass:&#039;java.lang.System&#039;) arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized. (see [[#Arrays | Arrays]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6221</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6221"/>
		<updated>2016-06-01T09:06:28Z</updated>

		<summary type="html">&lt;p&gt;Javogel: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; on the smalltal side matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array on the java side)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := java getJavaClass:&#039;[I&#039; new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := java getJavaClass:&#039;[I&#039; new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := java getJavaClass:&#039;[S&#039; new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    java getJavaClass:&#039;java.lang.System&#039; arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized. (see [[#Arrays | Arrays]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6220</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6220"/>
		<updated>2016-06-01T08:53:40Z</updated>

		<summary type="html">&lt;p&gt;Javogel: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== Passing Smalltalk Collections to Java ===&lt;br /&gt;
&lt;br /&gt;
Smalltalk collections can now be directly passed to Java with a few limitations:&lt;br /&gt;
* The identity of the Smalltalk collections will not be preserved. passing the same collection to java multiple times will result in multiple instances of the same array on the java side. (to bypass this instantiate a new java array)&lt;br /&gt;
* Smalltalk collections need to be explicitly converted to the correct type (e.g. a &amp;lt;CODE&amp;gt;SignedIntegerArray&amp;lt;/CODE&amp;gt; matches an &amp;lt;CODE&amp;gt;int&amp;lt;/CODE&amp;gt;-Array)&lt;br /&gt;
&lt;br /&gt;
=== Instantiating Arrays ===&lt;br /&gt;
&lt;br /&gt;
The new bridge supports instantiation of arrays (whereas previously reflection had to be used to instantiate arrays from the smalltalk side).&lt;br /&gt;
There are two ways an array can be instantiated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := java getJavaClass:&#039;[I&#039; new:#[ 1 2 3 4 5 ] asSignedIntegerArray. &amp;quot;/ instantiates a new array of type &amp;quot;int&amp;quot; with the contents &amp;quot;1, 2, 3, 4, 5&amp;quot;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray := java getJavaClass:&#039;[I&#039; new:10. &amp;quot;/ instantiates a new empty array of type &amp;quot;int&amp;quot; with 10 elements.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Arrays ===&lt;br /&gt;
&lt;br /&gt;
To achieve better performance, arrays of primitive types smaller than a certain size have their contents cached on the smalltalk side:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    intArray contents at:1. &amp;quot;/ this will access the first element of the cached contents.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
large arrays are not serialized immediately. large arrays will be serialized when &amp;lt;CODE&amp;gt;contents&amp;lt;/CODE&amp;gt; is called the first time.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Java arrays are not immutable. changes to an array are not reflected in the cached contents. if an array has been updated, call &amp;lt;CODE&amp;gt;_refresh&amp;lt;/CODE&amp;gt; on the array to update the cache. arrays will not be updated automatically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    shortArray := java getJavaClass:&#039;[S&#039; new:10. &amp;quot;/ new empty &amp;quot;short&amp;quot; array with 10 elements&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/ copy the contents of the smalltalk collection to the newly instantiated short array&lt;br /&gt;
    java getJavaClass:&#039;java.lang.System&#039; arraycopy:(#[ 1 2 3 ] asSignedWordArray) srcPos:0 dest:shortArray destPos:0 length:3.&lt;br /&gt;
&lt;br /&gt;
    shortArray at:0. &amp;quot;/ will reflect the changes made to the array&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will still return 0 because the cached contents have not been updated.&lt;br /&gt;
&lt;br /&gt;
    shortArray _refresh. &amp;quot;/ update the cached contents&lt;br /&gt;
&lt;br /&gt;
    shortArray contents at:1. &amp;quot;/ will now correctly reflect the changes made to the array&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized. (see [[#Arrays | Arrays]])&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6219</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6219"/>
		<updated>2016-06-01T07:41:21Z</updated>

		<summary type="html">&lt;p&gt;Javogel: #Activating the new Java Bridge&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Activating the new Java Bridge ==&lt;br /&gt;
To activate the new bridge in expecco navigate to &amp;lt;CODE&amp;gt;Extras -&amp;gt; Settings -&amp;gt; Plugins -&amp;gt; Java Bridge&amp;lt;/CODE&amp;gt; and tick the &#039;&#039;&#039;Use new Bridge (experimental)&#039;&#039;&#039; checkbox.&lt;br /&gt;
[[Datei:ActivateNewJavaBridge.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Attention:&#039;&#039;&#039; Ticking the checkbox will terminate all currently active Bridge connections due to incompatibilities between the new and the old version.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized.&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6204</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6204"/>
		<updated>2016-05-31T16:29:47Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Differences between v1 and v2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved. (see [[#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized.&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6203</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6203"/>
		<updated>2016-05-31T16:28:59Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Differences between v1 and v2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved. (see [[#API#JAVA::Java#instance protocol#boxedDo:aBlock | boxedDo]])&lt;br /&gt;
* Arrays of primitive types are serialized.&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6202</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6202"/>
		<updated>2016-05-31T16:10:47Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Differences between v1 and v2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved.&lt;br /&gt;
* Arrays of primitive types are serialized.&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced (also available for v1 since expecco v2.9). This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6201</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6201"/>
		<updated>2016-05-31T16:06:08Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* General */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple tcp ports to be open. This simplifies connection through ssh tunnels and firewalls.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved.&lt;br /&gt;
* Arrays of primitive types are serialized.&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced. This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6200</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6200"/>
		<updated>2016-05-31T15:59:07Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple ports to be open. Only one port is required.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved.&lt;br /&gt;
* Arrays of primitive types are serialized.&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced. This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6199</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6199"/>
		<updated>2016-05-31T15:58:28Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge (which is as of expecco v2.9 still experimental).&lt;br /&gt;
The article for version 1 can be found [[Java Interface Library | here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple ports to be open. Only one port is required.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved.&lt;br /&gt;
* Arrays of primitive types are serialized.&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced. This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&amp;lt;br&amp;gt;&lt;br /&gt;
Back to [[OnlineDocumentation#Library and Plugin Overview|Online Documentation]].&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6198</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6198"/>
		<updated>2016-05-31T15:34:14Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* General */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge. The article for version 1 can be found [[Java Interface Library | here]]&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple ports to be open. Only one port is required.&lt;br /&gt;
* The identity of strings and primitve wrappers can be preserved.&lt;br /&gt;
* Arrays of primitive types are serialized.&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced. This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&amp;lt;br&amp;gt;&lt;br /&gt;
Back to [[OnlineDocumentation#Library and Plugin Overview|Online Documentation]].&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6197</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6197"/>
		<updated>2016-05-31T15:31:52Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Differences between v1 and v2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge. The article for version 1 can be found [[Java Interface Library | here]]&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===General===&lt;br /&gt;
* A Java Bridge instance no longer requires multiple ports to be open. Only one port is required.&lt;br /&gt;
* The identity of strings and autoboxed values can be preserved.&lt;br /&gt;
* Arrays of primitive types are serialized.&lt;br /&gt;
&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
* A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced. This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&amp;lt;br&amp;gt;&lt;br /&gt;
Back to [[OnlineDocumentation#Library and Plugin Overview|Online Documentation]].&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6196</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6196"/>
		<updated>2016-05-31T15:26:07Z</updated>

		<summary type="html">&lt;p&gt;Javogel: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This article discusses version 2 of the Java Bridge. The article for version 1 can be found [[Java Interface Library | here]]&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | for details see Remote Dynamic Code Injection]]&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Differences between v1 and v2==&lt;br /&gt;
===Loading Applications===&lt;br /&gt;
* &#039;&#039;&#039;addJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;addToClassPath:&#039;&#039;&#039; are still supported but have been deprecated. use &#039;&#039;&#039;loadJar:&#039;&#039;&#039;, &#039;&#039;&#039;loadJarByPath:&#039;&#039;&#039; and &#039;&#039;&#039;loadExtension:&#039;&#039;&#039; instead. The new methods no longer require the jar or library to be present on the remote machine.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Loading Applications (from the expecco side) | v1]] [[#Loading Applications (from the expecco side) | v2]]&lt;br /&gt;
  &lt;br /&gt;
===Java Class Access===&lt;br /&gt;
* Sending the fully qualified name with instances of &amp;quot;.&amp;quot; replaced by &amp;quot;_&amp;quot; to the bridge handle is still supported, but not recommended.&lt;br /&gt;
A new method &#039;&#039;&#039;getJavaClass:&#039;&#039;&#039; has been introduced. This solves the side effects of &amp;quot;_&amp;quot; being a legal character in class names.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Java Package Import and Class Access | v1]] [[#Java Class Access | v2]]&lt;br /&gt;
===Callbacks from Java===&lt;br /&gt;
* A new method &#039;&#039;&#039;implementMethod:as:&#039;&#039;&#039; has been introduced to assign callback blocks to method names. The old way is still supported.&lt;br /&gt;
&lt;br /&gt;
[[Java Interface Library#Callbacks from Java | v1]] [[#Callbacks from Java | v2]]&lt;br /&gt;
&lt;br /&gt;
===Remote Dynamic Code Injection===&lt;br /&gt;
* Code injection was problematic in v1 and has now been re-implemented in v2.&lt;br /&gt;
&lt;br /&gt;
[[#Remote Dynamic Code Injection | v2]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&amp;lt;br&amp;gt;&lt;br /&gt;
Back to [[OnlineDocumentation#Library and Plugin Overview|Online Documentation]].&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6195</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6195"/>
		<updated>2016-05-31T14:28:06Z</updated>

		<summary type="html">&lt;p&gt;Javogel: /* Java Class Access */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading precompiled java bytecode:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java bytecode&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&amp;lt;br&amp;gt;&lt;br /&gt;
Back to [[OnlineDocumentation#Library and Plugin Overview|Online Documentation]].&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
	<entry>
		<id>https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6194</id>
		<title>Java Interface Library v2</title>
		<link rel="alternate" type="text/html" href="https://doc.expecco.de/index.php?title=Java_Interface_Library_v2&amp;diff=6194"/>
		<updated>2016-05-31T14:25:59Z</updated>

		<summary type="html">&lt;p&gt;Javogel: Die Seite wurde neu angelegt: „== Introduction ==  The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).  The access…“&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Java Interface library (&amp;quot;Java Bridge&amp;quot;) is used to interact with Java objects inside an external Java Virtual Machine (JVM).&lt;br /&gt;
&lt;br /&gt;
The access to Java objects, classes and programs is done via a framework called &amp;quot;&#039;&#039;Java-Bridge&#039;&#039;&amp;quot;, which is similar in operation to the [[DOT NET Interface Library| dotNET bridge]]. This framework implements transparent forwarding of method (virtual function) calls to Java objects, which exist in a local or remote Java Virtual Machine (JVM). Also, return values, callBacks and exception information are passed back from the Java program to expecco. This is done by a proxy-object mechanism, which catches all function calls, wraps the arguments, sends a datagram to the other bridge side, awaits the reply and returns the result to the original caller. Thus, remote procedure calls are almost completely transparent to the Smalltalk/JavaScript code inside expecco. In your elementary code, you can write remote function calls as if they were to local objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this document describes the low level framework, which implements the bridge communication. A much easier to use high level interface (called &amp;quot;Groovy Blocks&amp;quot;) exists and should normally be used, if you need access to a java application&#039;s internals during testing.&lt;br /&gt;
Please take a look at &lt;br /&gt;
[[Expecco_API#Groovy_Elementary_Blocks | &amp;quot;Groovy Block API&amp;quot;]]&lt;br /&gt;
and [[Testing Java Applications using Groovy blocks/en | &amp;quot;Testing Java Applications using Groovy blocks&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;br /&gt;
&lt;br /&gt;
On the expecco side, the bridge consists of a number of Proxy objects,&lt;br /&gt;
which behave like regular Smalltalk objects as seen from elementary expecco code.&lt;br /&gt;
However, instead of performing an action when one of their methods (virtual functions) is called,&lt;br /&gt;
they send a message over a socket connection to the Java VM (actually: to a bridge software inside the&lt;br /&gt;
Java application) which decodes the message and sends it to the destination Java object.&lt;br /&gt;
The same is done in reverse direction with the return value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+ &lt;br /&gt;
 |                     |     |          |     |                |      |                |     |   Java  |&lt;br /&gt;
 |       expecco       |----&amp;gt;|  Proxy   |----&amp;gt;|      Bridge    |==&amp;gt;&amp;gt;==|      Bridge    |----&amp;gt;|  Object |&lt;br /&gt;
 |  (elementary code)  |&amp;lt;----|          |&amp;lt;----| (expecco side) |==&amp;lt;&amp;lt;==|   (Java side)  |&amp;lt;----|         |&lt;br /&gt;
 +---------------------+     +----------+     +----------------+      +----------------+     +---------+&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This setup allows for transparent communication with objects inside a local or remote Java VM. This may be either utility functions which happen to be convenient for the test application (for example: file parsing, syntax analysis, protocol implementaitons or access to specific hardware via driver libraries), or the tested application in the system under test itself.&lt;br /&gt;
&lt;br /&gt;
Especially, it allows for the objects of the tested application to be looked into, manipulated and for functions of it or underlying frameworks to be called.&lt;br /&gt;
&lt;br /&gt;
For expecco code, we tried to make this as transparent as possible, however there are a few exceptions, when function names need to be translated (for example, because the function naming syntax is different) or due to the fact that for some operations no corresponding name exists in Smalltalk (array access, for example).&lt;br /&gt;
&lt;br /&gt;
Of course, multiple such connections can coexist and be served/handled in parallel. This enables an expecco test suite to communicate with both sides of a tested client-server Java application, for example.&lt;br /&gt;
&lt;br /&gt;
Notice that a similar mechanism is used in expecco to communicate with DOTNET, Qt (C++) and C applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Initializing / Releasing the Bridge ==&lt;br /&gt;
&lt;br /&gt;
Before any communication can take place between expecco and any Java object, the Java side of the bridge has to be started, and a communication has to be established.&lt;br /&gt;
In expecco, all of the bridge classes are found in the &amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt; namespace.&lt;br /&gt;
&lt;br /&gt;
The main interface class is &amp;quot;&amp;lt;CODE&amp;gt;Java&amp;lt;/CODE&amp;gt;&amp;quot;, in the &amp;quot;&amp;lt;CODE&amp;gt;JAVA&amp;lt;/CODE&amp;gt;&amp;quot; namespace:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java := JAVA::Java newWithServer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or (in JavaScript):&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java = JAVA::Java.newWithServer();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This starts the Java-side of the bridge (actually executes java as a background command) on the local machine, and establishes a connection to that JVM.&lt;br /&gt;
&lt;br /&gt;
The bridge connection should be closed, if the bridge is no longer needed. Release the bridge with:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java.closeBridge();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
which terminates the connection.&lt;br /&gt;
&lt;br /&gt;
=== Start and Connect to a Remote Machine ===&lt;br /&gt;
The above started a Java VM and connected to it. Alternatively, you may want to connect to an already running Java program (typically, your system under test). &lt;br /&gt;
In order for your program to be reachable, it must allow for the bridge to connect to it via a special bridge-connection, which is a socket connection. For this, it must execute the server code found in the &amp;quot;JavaBridge.jar&amp;quot; file. To pass [[#Startup Parameters| parameters]] you will typically start it from a command line or a little shell/batch script.&lt;br /&gt;
&lt;br /&gt;
Call (on the shell/cmd level):&lt;br /&gt;
    $ java -jar &amp;lt;PATH_TO_JAVABRIDGE.JAR&amp;gt; &amp;lt;PARAMETERS&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;JavaBridge.jar&amp;quot; file is found in the installation directory of expecco at:&lt;br /&gt;
    \exept\expecco\packages\exept\bridgeFramework\javaBridge\javaBin\JavaBridge.jar&lt;br /&gt;
&lt;br /&gt;
The Javabridge code allows for either side to initiate the connection - i.e. it can be run in server mode,&lt;br /&gt;
where it awaits an incoming connection, or in client mode, where it actively initiates a connection.&lt;br /&gt;
After connection establishment, there is no difference in the operation of the bridge, however depending on the setup of your network infrastructure, especially firewalls, either mechanism may be easier to be used. &lt;br /&gt;
&lt;br /&gt;
Assuming that the bridge is already running on the remote machine as server (default),&lt;br /&gt;
use &amp;lt;CODE&amp;gt;newConnectedTo:port:withTimeout:&amp;lt;/CODE&amp;gt; to connect to it from the expecco side:&lt;br /&gt;
    java := JAVA::Java newConnectedTo:&#039;myHost&#039; port:4567 withTimeout:30.&lt;br /&gt;
This will initiate the connection setup from the expecco side.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if started in client mode use &amp;lt;CODE&amp;gt;newWaitingForConnectOnHost:port:withTimeout:&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
    java := JAVA::Java newWaitingForConnectOnHost:&#039;localhost&#039; port:4567 withTimeout:30.&lt;br /&gt;
which will open a port on the expecco side, and wait until the Java side connects to it.&lt;br /&gt;
[[#JAVA::Java|For more details see API]]&lt;br /&gt;
&lt;br /&gt;
====Startup Parameters====&lt;br /&gt;
You can use any parameter in any order.&lt;br /&gt;
If no parameter is used then the bridge listenes on port 14014 in server mode without keepAlive.&lt;br /&gt;
;&amp;lt;code&amp;gt;-help&amp;lt;/code&amp;gt;&lt;br /&gt;
* Shows the help&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-ip &amp;lt;aHostnameOrIP&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* In client mode this specifies the ip or hostname to connect to. In server mode this specifies the ip or hostname to bind to.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-port &amp;lt;aPortNumber&amp;gt; &amp;lt;/code&amp;gt;    &lt;br /&gt;
* In client mode this specifies the port to connect to. In server mode this specifies the listening port.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-asClient&amp;lt;/code&amp;gt;&lt;br /&gt;
* The bridge will start in client mode. If not set the bridge is started in server mode.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;-keepAlive&amp;lt;/code&amp;gt;&lt;br /&gt;
* By setting this flag the bridge will not exit after the connection has closed and starts to connect or listen again. Otherwise, it will run for a single connection session only.&lt;br /&gt;
&lt;br /&gt;
== Loading Applications (from the expecco side) ==&lt;br /&gt;
&lt;br /&gt;
Normally, you would start your application with the required command line arguments and include the javabridge as jar there. However, in some situations, it may be required to dynamically load other jars into the connected Java VM.&lt;br /&gt;
By accessing a classloader, additional classes, or applications as contained in JAR files can be loaded from the expecco side.&lt;br /&gt;
&lt;br /&gt;
This can be used to add a JAR file to the classloader:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJarByPath:&#039;pathToJarFile&#039;. &amp;quot;/ assuming &amp;quot;java&amp;quot; is a handle as returned from the above connect&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if the JAR file is loaded in memory:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadJar:jarByteArray. &amp;quot;/ where &amp;quot;jarByteArray&amp;quot; is a ByteArray containing the JAR file&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the JVM is on a remote machine, the path is resolved on the expecco side and the JAR will be transferred to the remote side.&lt;br /&gt;
&lt;br /&gt;
Should transferring the JAR be too much overhead, an additional mechanism is provided:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java loadExtension:&#039;pathToJarFile&#039;.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
this will cache the JAR file on the bridge side and eliminate the overhead of subsequent invocations.&lt;br /&gt;
&lt;br /&gt;
== Java Class Access ==&lt;br /&gt;
&lt;br /&gt;
This following code sample simply references (i.e. accesses) the class &amp;quot;JFrame&amp;quot; from the &amp;lt;CODE&amp;gt;&amp;quot;javax.swing&amp;quot;&amp;lt;/CODE&amp;gt; package. The fully specified class name in Java would be &amp;lt;CODE&amp;gt;&amp;quot;javax.swing.JFrame&amp;quot;&amp;lt;/CODE&amp;gt;:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if you want to load multiple classes:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java import:&#039;javax.swing.JFrame&#039; import:&#039;javax.swing.JButton&#039; &lt;br /&gt;
    do:[:jFrame :jButton | &amp;quot;do something with JButton and JFrame&amp;quot; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
v2 also supports dynamic compilation of java source code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromSource:&#039;classSource&#039;. &amp;quot;/ where classSource is a String containing valid java source code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and loading already compiled java byte code:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;className&#039; fromByteCode:byteCode. &amp;quot;/ where byteCode is a ByteArray containing valid java byte code&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Instantiating a Class ==&lt;br /&gt;
&lt;br /&gt;
Object instances are created via the &amp;quot;new&amp;quot;-message, sent to a proxy of a Java class. You can get this proxy as described in the previous section. For example, if we want to create a new instance of a &amp;quot;JFrame&amp;quot; and a &amp;quot;JButton&amp;quot;, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java getJavaClass:&#039;javax.swing.JButton&#039; new. &lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The first statement results in a proxy object for the &amp;quot;JFrame&amp;quot; instance. The second line results in a proxy for a new &amp;quot;JButton&amp;quot; object.&lt;br /&gt;
&lt;br /&gt;
== Calling Instance Methods ==&lt;br /&gt;
&lt;br /&gt;
A method call is done by a message send to a proxy object, where the message is the method name to call. For example, to call the &amp;lt;CODE&amp;gt;&amp;quot;setVisible(boolean isVisible)&amp;quot;&amp;lt;/CODE&amp;gt; method on the above JFrame object, we send the following message to that proxy:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setVisible:true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As method naming is different in Smalltalk and Java, a dynamic translation is applied when sending messages from Smalltalk code to a remote Java object. Smalltalk has arguments embedded inside parts of a so called &amp;quot;keyword&amp;quot; message, such as in &amp;quot;receiver foo:arg1 bar:arg2&amp;quot;, where &amp;quot;foo:bar:&amp;quot; would be the message name, and &amp;quot;arg1&amp;quot;, &amp;quot;arg2&amp;quot; be the arguments.&lt;br /&gt;
&lt;br /&gt;
In Java, the message name is a single identifier, and arguments are written in an argument list after the message name: &amp;quot;receiver.fooBar(arg1, arg2)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
When message names are translated, only the very first part of the Smalltalk keyword message is taken (&amp;quot;foo&amp;quot; in the above example), and the remaining parts are simpl ignored. Thus, any of the Smalltalk messages &amp;quot;foo:bar:&amp;quot;, &amp;quot;foo:xxxx:&amp;quot; and &amp;quot;foo:_:&amp;quot; (a single underscore as second part) all translate to the same Java message name &amp;quot;foo()&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Therefore, to call a method with more than one argument, like &amp;lt;CODE&amp;gt;&amp;quot;setSize(int width,int height)&amp;quot;&amp;lt;/CODE&amp;gt; on a JFrame instance we can write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
but just as well:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setSize:300 anyWord:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or any other selector with &amp;quot;setSize:&amp;quot; as its first component. &lt;br /&gt;
The selector translation mechanism simply takes the first part as the Java selector.&lt;br /&gt;
&lt;br /&gt;
In practice, you should take a reasonably descriptive name, such as: &amp;quot;frame setSize:300 height:200&amp;quot; or only the Java name as first part, such as &amp;quot;frame setSize:300 _:200&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
For a call of a method with 4 arguments, we could write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 y:50 width:300 height:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
or:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setBounds:100 _:50 _:300 _:200.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also here the &amp;lt;CODE&amp;gt;&amp;quot;y: width: height:&amp;quot;&amp;lt;/CODE&amp;gt; can be named as you want, but the first part must be &amp;quot;setBounds:&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Calling Static Methods ===&lt;br /&gt;
&lt;br /&gt;
Calling a static method on an object is not different from calling non static methods. In most cases you will call a static method not on an object but directly on the class. This is done by using the class object as &amp;quot;receiver&amp;quot; of the message. For example, if we want to call the static method &amp;lt;CODE&amp;gt;&amp;quot;isDefaultLookAndFeelDecorated()&amp;quot;&amp;lt;/CODE&amp;gt; of the JFrame class, write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    java getJavaClass:&#039;javax.swing.JFrame&#039; isDefaultLookAndFeelDecorated.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing Fields ==&lt;br /&gt;
&lt;br /&gt;
As Smalltalk does not support access of an object&#039;s fields from the outside (it is fully encapsulated, and access from outside is ONLY allowed via getters/setters), an additional translation mechanism is provided for field access.&lt;br /&gt;
 &lt;br /&gt;
Accessing a field of a Java object or a class&#039;s static field uses the same syntax as method calls. To access a field, send a message where the message is the field name. To access the field of a class the field must be static. &lt;br /&gt;
For example, to get the value of the static field named &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; of the &amp;quot;JFrame&amp;quot; class, write&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := java getJavaClass:&#039;javax.swing.JFrame&#039; EXIT_ON_CLOSE.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The variable &amp;quot;value&amp;quot; now holds the integer value of the &amp;lt;CODE&amp;gt;&amp;quot;EXIT_ON_CLOSE&amp;quot;&amp;lt;/CODE&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Assuming that a &amp;quot;JFrame&amp;quot; object had a field named &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt;, to access this field, we&#039;d have to write:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    value := frame myField.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
and &amp;quot;value&amp;quot; now holding whatever &amp;lt;CODE&amp;gt;&amp;quot;myField&amp;quot;&amp;lt;/CODE&amp;gt; returns. This could be another object reference or any primitive value.&lt;br /&gt;
&lt;br /&gt;
=== Setting Fields ===&lt;br /&gt;
To set a field, use a setter-like method call:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame myField: newValue.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Name Conflicts ===&lt;br /&gt;
&lt;br /&gt;
In very rare situations, a Java object may contain both a field and a method by the same name.&lt;br /&gt;
In this case, the Smalltalk code cannot depend on the dynamic translation mechanism (which actually looks for either a field or method name to match), but instead make it explicit, which operation is wanted:&lt;br /&gt;
Use:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame getFieldByName:&#039;myField&#039;&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to read a field, and:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    frame setFieldByName:&#039;myField&#039; value:123.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
to write the field.&lt;br /&gt;
&lt;br /&gt;
If there are both fields and methods by the same name, and you use the non-explicit call,&lt;br /&gt;
the bridge will always assume that you want to call the getter/setter and perform a method call.&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== Callbacks from Java ==&lt;br /&gt;
Sometimes you may want to execute a piece of Smalltalk code during the execution of the Java code. For example, we may want to install a Smalltalk observer to be notified when a button on a Java GUI was pressed,&lt;br /&gt;
or some other callback from a Java framework. &lt;br /&gt;
&lt;br /&gt;
The following code registers a Smalltalk block as a callback for a mouse press event of a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    listener := MouseListener new.&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;Hello from Java&#039; ].&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The above creates the callback and the listener, which can now be registered on a button:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
Now, with the press of the button in the Java GUI, the Smalltalk block will be executed and writes &amp;lt;CODE&amp;gt;&amp;quot;Hello from Java&amp;quot;&amp;lt;/CODE&amp;gt; to the Smalltalk console.&lt;br /&gt;
Of course, any other (Smalltalk-)action code can be made to run via this mechanism. &lt;br /&gt;
&lt;br /&gt;
But be aware, that the callback is possibly called later (when the elementary action, which installed the code, has already finished long ago). Also, as the Java code execution is not synchronized with any of your expecco action execution, the callback may also be called at any arbutrary time, even at times when no activity at all is executed on the expecco side. If you forget to cleanup, and the Java object is still alive, it may even be called after you have finished your test run (if you keep the java bridge handle around and alive, for example in an expecoo environment variable).&lt;br /&gt;
&lt;br /&gt;
So the Smalltalk callback code should not depend on any particular elementary action to be currently running, but instead in most cases write some information into a shared data container, preferably an instance of &amp;quot;SharedQueue&amp;quot;, which is thread safe. Then, some other action (an elementary action) would read-wait and read events from that shared queue.&lt;br /&gt;
&lt;br /&gt;
make sure that your test correctly cleans up any such callbacks afterwards. As a last resort, use the &amp;quot;Shutdown Bridge Connection&amp;quot; menu item, which closes the bridge connection, and thereby - as a side effect - removes any leftover callbacks. It does close the connection, and any remaining java proxy object handles become invalid, though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, using an observer could look like:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
    observer implementMethod:&#039;update&#039;&lt;br /&gt;
        as:[:sourceObservable :argument| &lt;br /&gt;
            argument notNil ifTrue:[ Transcript showCR:(&#039;I was notifyed with: &#039;,argument toString) ]&lt;br /&gt;
            ifFalse:[ Transcript showCR:&#039;I was notifyed and the argument was nil&#039; ].&lt;br /&gt;
        ].&lt;br /&gt;
    myObservable addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
When the observable calls &amp;lt;CODE&amp;gt;&amp;quot;notifyObservers&amp;quot;&amp;lt;/CODE&amp;gt;, the Smalltalk block will be executed.&lt;br /&gt;
&lt;br /&gt;
== Exceptions in Java ==&lt;br /&gt;
Exceptions on the Java side are signalled back to the Smalltalk side and raise a corresponding exception there. However, on the Java side, the call stack which lead to the exception has already been unwound at that time (Java exceptions are not proceedable). So a proceed in the Smalltalk-side exception handler only affects the Smalltalk caller, but may leave the Java side in an undefined state.&lt;br /&gt;
&lt;br /&gt;
== Remote Dynamic Code Injection ==&lt;br /&gt;
Sometimes we might want to execute code without the overhead of many rpc-messages being sent over the bridge, especially if we want to do something time critical, or want to make execution time measurements. &lt;br /&gt;
For this, it is possible to load a class at runtime into the JavaVM. The code of the class will first be sent to the JavaVM, which will compile and load the generated byte code. Then, the class can be accessed via the bridge just like any other preloaded class. &lt;br /&gt;
&lt;br /&gt;
The following example specifies the class code in a smalltalk string and inject the code into the JavaVM. Then an instance of that new class will be instantiated and finally a method of the object instance will be invoked:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
   classCode := &#039;&lt;br /&gt;
        package myPackage.test;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.BorderLayout;&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.util.Calendar;&lt;br /&gt;
&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
        import javax.swing.JLabel;;&lt;br /&gt;
&lt;br /&gt;
        public class MyInjectedClass {&lt;br /&gt;
            public void createSampleApp(){&lt;br /&gt;
                JFrame frame=new JFrame(&amp;quot;Hello im injected&amp;quot;);&lt;br /&gt;
                frame.setLayout(new BorderLayout());&lt;br /&gt;
                JButton button=new JButton(&amp;quot;click me&amp;quot;);&lt;br /&gt;
                final JLabel label=new JLabel(&amp;quot;Text&amp;quot;);&lt;br /&gt;
                button.addMouseListener(new MouseAdapter() {&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseClicked(MouseEvent e) {&lt;br /&gt;
                     label.setText(Calendar.getInstance().getTime().toString());&lt;br /&gt;
                    }&lt;br /&gt;
                });&lt;br /&gt;
                frame.add(label,BorderLayout.NORTH);&lt;br /&gt;
                frame.add(button,BorderLayout.SOUTH);&lt;br /&gt;
                frame.setSize(300, 300);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }   &lt;br /&gt;
    &#039;.&lt;br /&gt;
    classObject := java getJavaClass:&#039;myPackage.test.MyInjectedClass&#039; fromSource:classCode.&lt;br /&gt;
    classObject new createSampleApp.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
After the code has been injected, a new instance of &amp;lt;CODE&amp;gt;&amp;quot;MyInjectedClass&amp;quot;&amp;lt;/CODE&amp;gt; is created and the &amp;lt;CODE&amp;gt;&amp;quot;createSampleApp&amp;quot;&amp;lt;/CODE&amp;gt; method is called. Because the example implements a GUI, a Java window will open, containing a label and a button. Clicking the button displays the current time in the label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Redefining dynamic injected classes ===&lt;br /&gt;
&lt;br /&gt;
The Java bridge makes it possible to even redefine any previously injected class during runtime, without a need to shut down and restart the application.&lt;br /&gt;
Any new instance created after the re-injection will have the new defined behavior. Static methods will be affected immediately after a re-inject.&lt;br /&gt;
Notice that existing old instances are not affected by a changed class definition - only new instances will be.&lt;br /&gt;
&lt;br /&gt;
Here is an example where a class named &amp;lt;CODE&amp;gt;&amp;quot;MyCounter&amp;quot;&amp;lt;/CODE&amp;gt; with a static method &amp;lt;CODE&amp;gt;&amp;quot;raiseCount&amp;quot;&amp;lt;/CODE&amp;gt; will be redefined. Initially the method increments a counter by 1. After redefining and re-injecting the method, the behavior will change to decrement the counter by 1.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    currentCount:=0.&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyCounter&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count+=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/starting a thread which calls the raiseCount method of the class MyCounter&lt;br /&gt;
    proc:=[&lt;br /&gt;
        [true]whileTrue:[&lt;br /&gt;
            currentCount := java myPackage_test_MyCounter raiseCount.&lt;br /&gt;
            Transcript showCR:currentCount.&lt;br /&gt;
            Delay waitForSeconds:1.&lt;br /&gt;
        ].&lt;br /&gt;
    ]forkAt:7.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/redefining MyCounter&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        public class %2 {&lt;br /&gt;
            private static int count=%3;&lt;br /&gt;
&lt;br /&gt;
            public static int raiseCount(){&lt;br /&gt;
                return count-=1;&lt;br /&gt;
            }&lt;br /&gt;
        }    &lt;br /&gt;
    &#039;bindWith:packageName with:className with:currentCount.&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    Delay waitForSeconds:10.&lt;br /&gt;
    &lt;br /&gt;
    proc terminate.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Code Sources for Class injection ===&lt;br /&gt;
There are 3 possible types of class code sources which we can use to inject the code. In the examples before we directly used a simple &amp;lt;CODE&amp;gt;&amp;quot;String&amp;quot;&amp;lt;/CODE&amp;gt; in the smalltalk code as a code source. We also can use a &amp;lt;CODE&amp;gt;&amp;quot;*.java&amp;quot;&amp;lt;/CODE&amp;gt; file as source. Just read the content of the file into a string and use it as in the previous examples.&lt;br /&gt;
&lt;br /&gt;
To inject a &amp;lt;CODE&amp;gt;&amp;quot;*.class&amp;quot;&amp;lt;/CODE&amp;gt; file use the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
java getJavaClass:&#039;the.class.name&#039; fromByteCode:classCode.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This example creates a JFrame with one button inside. Then a Mouselistener is created and registered on the button. When the button is pressed, the title of the window changes to &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows the message &amp;lt;CODE&amp;gt;&amp;quot;onPressed&amp;quot;&amp;lt;/CODE&amp;gt;. When the button is released, the title of the window is changed to &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt; and the Transcript shows &amp;lt;CODE&amp;gt;&amp;quot;onReleased&amp;quot;&amp;lt;/CODE&amp;gt;. If the button lost the mouse focus, the bridge will exit (disconnect), and the Java side is terminated.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    |java frame button listener|&lt;br /&gt;
 &lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new frame and button object&lt;br /&gt;
    frame := java getJavaClass:&#039;javax.swing.JFrame&#039; new.&lt;br /&gt;
    button := java JButton new:&#039;Click&#039;.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a new mouse listener and adding callback blocks&lt;br /&gt;
    listener := java MouseListener new.&lt;br /&gt;
&lt;br /&gt;
    listener implementMethod:&#039;mousePressed&#039; as:[ Transcript showCR:&#039;onPress&#039;. frame setTitle:&#039;onPressed&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseReleased&#039; as:[ Transcript showCR:&#039;onReleased&#039;. frame setTitle:&#039;onReleased&#039; ].&lt;br /&gt;
    listener implementMethod:&#039;mouseExited&#039; as:[ Transcript showCR:&#039;onExited&#039;. java closeBridge. ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the mouse listener on the button and make the frame with button visible&lt;br /&gt;
    button addMouseListener:listener.&lt;br /&gt;
    frame add:button.&lt;br /&gt;
    frame setSize:300 y:100.&lt;br /&gt;
    frame setVisible: true.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=== Simple RMI Call ===&lt;br /&gt;
The next example is written in JavaScript syntax. It shows how an RMI host is queried for an RMI object and then the &amp;quot;getHello&amp;quot; method is called on it. The following code is not directly executable: you must replace the path to the compiled MyRemoteObject class with the directory containing the class file, and replace the RMI host name with a real one in your network:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    var java, reg, myRemoteObject, callResult;&lt;br /&gt;
&lt;br /&gt;
    // Start a local JavaVM and connect the Communication-Bridge to it&lt;br /&gt;
    java = JAVA::Java.singletonInstance;  &lt;br /&gt;
&lt;br /&gt;
    // Add the class path where the description class of the shared RMI object can be found. RMI could also load the class from the server, but then we would need a RMI SecurityManager with download rights&lt;br /&gt;
    java.bridgeSide.addClassSearchPath(&amp;quot;my\added\class\path&amp;quot;); &lt;br /&gt;
&lt;br /&gt;
    // java.rmi.registry.LocateRegistry gets us the RMI Registry on the host&lt;br /&gt;
    reg = java.java_rmi_registry_LocateRegistry.getRegistry(&amp;quot;rmiHost&amp;quot;,4567);  &lt;br /&gt;
&lt;br /&gt;
    // get the rmi object&lt;br /&gt;
    myRemoteObject = reg.lookup(&amp;quot;myObject&amp;quot;);   &lt;br /&gt;
&lt;br /&gt;
    // get the hello string that was written by the RMI server into the rmi object&lt;br /&gt;
    callResult = myRemoteObject.getHello();&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The RMI server written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
RMIServer.java:&lt;br /&gt;
&lt;br /&gt;
    import java.rmi.AlreadyBoundException;&lt;br /&gt;
    import java.rmi.RemoteException;&lt;br /&gt;
    import java.rmi.registry.LocateRegistry;&lt;br /&gt;
    import java.rmi.registry.Registry;&lt;br /&gt;
&lt;br /&gt;
    public class RMIServer {&lt;br /&gt;
&lt;br /&gt;
        public static void &lt;br /&gt;
        main( String[] args ) throws RemoteException, AlreadyBoundException, InterruptedException {&lt;br /&gt;
            int port = 4567;&lt;br /&gt;
&lt;br /&gt;
            if(args.length &amp;gt; 0)&lt;br /&gt;
                port = Integer.parseInt(args[0]);&lt;br /&gt;
                    &lt;br /&gt;
            Registry reg = LocateRegistry.createRegistry(port);&lt;br /&gt;
            reg.bind(&amp;quot;myObject&amp;quot;, new MyRemoteObject(&amp;quot;Hello from RMIServer: &amp;quot;+port));&lt;br /&gt;
            while(true) {&lt;br /&gt;
                Thread.sleep(100);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
The implementation of the shared RMI object written in Java:&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
MyRemoteObject.java&lt;br /&gt;
    import java.io.Serializable;&lt;br /&gt;
    import java.rmi.Remote;&lt;br /&gt;
&lt;br /&gt;
    public class MyRemoteObject implements Remote,Serializable {&lt;br /&gt;
            &lt;br /&gt;
        private static final long serialVersionUID = 1L;&lt;br /&gt;
            &lt;br /&gt;
        private String hello;&lt;br /&gt;
            &lt;br /&gt;
        public MyRemoteObject(String hello) {&lt;br /&gt;
            this.hello = hello;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public String getHello(){&lt;br /&gt;
            return hello;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Now nearly the same example with injected code but first without callback.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode|&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge newWithServer.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class %2{&lt;br /&gt;
&lt;br /&gt;
            public static boolean doJavaExit=false;&lt;br /&gt;
&lt;br /&gt;
            public static void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        doJavaExit=true;&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
    &lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static start method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass start.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/idle until the field doJavaExit in the injected class returns true.&lt;br /&gt;
    [ MyExampleClass doJavaExit ]whileFalse:[&lt;br /&gt;
        Delay waitForSeconds:1.&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/exit the java vm&lt;br /&gt;
    java exitJava.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now the same code but with a callback to smalltalk via an Java observer.&lt;br /&gt;
&amp;lt;CODE&amp;gt;&amp;lt;PRE&amp;gt;&lt;br /&gt;
    | java packageName className classCode observer |&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/getting a bridge&lt;br /&gt;
    java := JAVA::JavaBridge startNew.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/defining a java class in a string&lt;br /&gt;
    packageName := &#039;myPackage.test&#039;.&lt;br /&gt;
    className := &#039;MyExampleClass&#039;.&lt;br /&gt;
    classCode := &#039;&lt;br /&gt;
        package %1;&lt;br /&gt;
&lt;br /&gt;
        import java.awt.event.MouseAdapter;&lt;br /&gt;
        import java.awt.event.MouseEvent;&lt;br /&gt;
        import java.awt.event.MouseListener;&lt;br /&gt;
        import java.util.Observable;&lt;br /&gt;
        import javax.swing.JButton;&lt;br /&gt;
        import javax.swing.JFrame;&lt;br /&gt;
&lt;br /&gt;
        public class MyExampleClass extends Observable{&lt;br /&gt;
            &lt;br /&gt;
            public static MyExampleClass anInstanceOfMe=new MyExampleClass();&lt;br /&gt;
            &lt;br /&gt;
            public static void main(){&lt;br /&gt;
                anInstanceOfMe.start();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            public void start(){&lt;br /&gt;
                final JFrame frame=new JFrame();&lt;br /&gt;
                JButton button=new JButton(&amp;quot;Click&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
                MouseListener listener=new MouseAdapter() { &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseReleased(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onReleased&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mousePressed(MouseEvent e) {&lt;br /&gt;
                        frame.setTitle(&amp;quot;onPressed&amp;quot;);&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onPressed&amp;quot;);             &lt;br /&gt;
                    }&lt;br /&gt;
                    &lt;br /&gt;
                    @Override&lt;br /&gt;
                    public void mouseExited(MouseEvent e) {&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.setChanged();&lt;br /&gt;
                        MyExampleClass.anInstanceOfMe.notifyObservers(&amp;quot;onExited&amp;quot;);&lt;br /&gt;
                    }           &lt;br /&gt;
                };&lt;br /&gt;
                &lt;br /&gt;
                button.addMouseListener(listener);&lt;br /&gt;
                frame.add(button);&lt;br /&gt;
                frame.setSize(300, 100);&lt;br /&gt;
                frame.setVisible(true);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    &#039;bindWith:packageName with:className.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/injecting the class into the java vm&lt;br /&gt;
    java RemoteClassInject injectClassCode:packageName className:className classCode:classCode.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/calling the static main method on the injected class&lt;br /&gt;
    java myPackage_test_MyExampleClass main.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/creating a java observer proxy on in smalltalk&lt;br /&gt;
    observer := java Observer new.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/implement the update method which is called by an observable, the code in the block will then be executed&lt;br /&gt;
    observer update:[:observable :argument| &lt;br /&gt;
        Transcript showCR:argument.&lt;br /&gt;
        &amp;quot;/do the exit if the argument is equal &#039;onExited&#039;&lt;br /&gt;
        (argument=#onExited) ifTrue:[ java exitJava ].&lt;br /&gt;
    ].&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;/register the observer to the observable we want to be notifyed from&lt;br /&gt;
    MyExampleClass anInstanceOfMe addObserver:observer.&lt;br /&gt;
&amp;lt;/PRE&amp;gt;&amp;lt;/CODE&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
===JAVA::Java===&lt;br /&gt;
====class protocol====&lt;br /&gt;
=====newWithServer=====&lt;br /&gt;
* This will start a local JavaVM, running the JavaBridge, and establishes a connection on a random free port.&lt;br /&gt;
: A new connected instance of JAVA::Java class is returned.&lt;br /&gt;
&lt;br /&gt;
=====newWithServerForJavaPath:aJavaPathFilename=====&lt;br /&gt;
* This is the same as newWithServer but the path to the java executable can be specified. The JavaVM version must be 1.6 or higher to run the JavaBridge.&lt;br /&gt;
:: &#039;&#039;&#039;aJavaPathFilename&#039;&#039;&#039; - must be a instance of Filename&lt;br /&gt;
    Smalltalk: JAVA::Java newWithServerForJavaPath:(&#039;my\path\to\java.executable&#039;asFilename).&lt;br /&gt;
    JavaScript: JAVA::Java.newWithServerForJavaPath(&amp;quot;my\path\to\java.executable&amp;quot;.asFilename());&lt;br /&gt;
&lt;br /&gt;
=====newConnectedTo:aHhostString port:aPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to connect to a running JavaBridge on the network. The JavaBridge must be started in server mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;aHostString&#039;&#039;&#039; - the hostname or IP of the remote machine as String&lt;br /&gt;
:: &#039;&#039;&#039;aPortNumber &#039;&#039;&#039; - the port on which the remote machine is listening as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number&lt;br /&gt;
    Smalltalk: JAVA::Java newConnectedTo:&#039;myHostName&#039; port:4567 withTimeout:30.&lt;br /&gt;
    JavaScript: JAVA::Java.newConnectedTo_port_withTimeout(&amp;quot;myHostName&amp;quot;,4567,30);&lt;br /&gt;
&lt;br /&gt;
=====newWaitingForConnectOnHost:aListeningAddressString port:aListeningPortNumber withTimeout:aTimeoutSeconds=====&lt;br /&gt;
* This is used to wait for incomming connecions of a running JavaBridge on the network. The JavaBridge must be started in client mode on the remote host. After connecting a new connected instance of JAVA::Java is returned.&lt;br /&gt;
:: &#039;&#039;&#039;deprecated: aListeningAddressString&#039;&#039;&#039; - no longer used. Can be nil.&lt;br /&gt;
:: &#039;&#039;&#039;aListeningPortNumber &#039;&#039;&#039; - the port on which to listen for incomming connections as Number&lt;br /&gt;
:: &#039;&#039;&#039;aTimeoutSeconds&#039;&#039;&#039; - a connect timeout in seconds as Number, If nil it will wait endless.&lt;br /&gt;
&lt;br /&gt;
=====singletonInstance=====&lt;br /&gt;
* This will call newWithServer for the first time and then always returns the same connected JAVA::Java instance until the connection was closed, then a new connected instance is returned.&lt;br /&gt;
&lt;br /&gt;
=====exitAllInstances=====&lt;br /&gt;
* This will close all instances of JAVA::Java.&lt;br /&gt;
&lt;br /&gt;
====instance protocol====&lt;br /&gt;
=====loadJarByPath:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====loadJar:aByteArray=====&lt;br /&gt;
* Adds a jar to the running JavaVM. If the jar depends on other jars or libraries you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aByteArrays&#039;&#039;&#039; - a ByteArray containing the JAR file.&lt;br /&gt;
&lt;br /&gt;
=====loadExtension:aPath=====&lt;br /&gt;
* Adds a jar to the running JavaVM and caches it for subsequent usages. If the jar depends on other jars you have to add them too.&lt;br /&gt;
:: &#039;&#039;&#039;aPath&#039;&#039;&#039; - the path to the jar file as String.&lt;br /&gt;
&lt;br /&gt;
=====boxedDo:aBlock=====&lt;br /&gt;
* Any autoboxed values in aBlock returned from the JavaVM are preserved as such. By default autoboxed values are automatically unboxed. This way, the identity of such objects is preserved.&lt;br /&gt;
&lt;br /&gt;
=====isConnected=====&lt;br /&gt;
* Tests if a connection is still established.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still established, else false.&lt;br /&gt;
&lt;br /&gt;
=====isAlive=====&lt;br /&gt;
* Tests if a connection is still responding.&lt;br /&gt;
:: &#039;&#039;&#039;return:&#039;&#039;&#039; - true if the connection is still alive, else false. Does a message round trip.&lt;br /&gt;
&lt;br /&gt;
=====closeBridge=====&lt;br /&gt;
* This will close the bridge connection. Depending on in which mode the JavaBridge was started, the JavaVM is also closed (-keepAlive startparameter not set).&lt;br /&gt;
&lt;br /&gt;
=====exit=====&lt;br /&gt;
* Same as closeBridge.&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
The [[DOT NET Interface Library| DOTNET Interface Plugin &amp;amp; Library]], which implements a likewise interface for .NET applications/libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr&amp;gt;&lt;br /&gt;
Back to [[Plugins]]&amp;lt;br&amp;gt;&lt;br /&gt;
Back to [[OnlineDocumentation#Library and Plugin Overview|Online Documentation]].&lt;/div&gt;</summary>
		<author><name>Javogel</name></author>
	</entry>
</feed>