This is a follow-up to the post A Toolchain for Integrating MetaTrader 4 and Java or Scala.
Success!
Using the method described in my previous post I have successfully invoked Java code from an Expert Advisor in MetaTrader 4. My single deviation from the original article is that I corrected the misspelling of "Interface" throughout.
I developed this solution in the following environment:
-
Mac OSX 10.6
-
Windows 7 Professional running under VirtualBox on the Mac
-
JDK 6 (1.6.0_15)
-
Eclipse Galileo (any IDE will do, or none at all)
-
Visual Studio 2005, SP1 for Vista and Windows 7
-
MetaTrader 4.00 Build 225
-
IKVM 0.40.0.1
Here is my method:
First, follow the steps as directed by this CodeGuru.com article. My tutorial assumes that you have already successfully completed all of the previous steps.
I created a simple Java project in Eclipse called MetaTraderImportTest. I then created a package com.corsairllc.mt4Import. You should use whatever you like. Keep in mind that the package declared here will be used as the namespace in the CppStdcallInterfaceWrapper.cpp file. The I created a new Java class within that package called JavaClass with the following code:
package com.corsairllc.mt4Import;
public class JavaClass {
public static byte[] Hello(byte[] name) {
String retval = new String(name) + ", hello from Java!";
return retval.getBytes();
}
}
I know that the method name violated method naming conventions but I am trying to make this class a drop-in replacement for the C# version used previously.
Now export a JAR of the project. I called mine JavaClass.jar (but in retrospect JavaAssembly.jar may have been a better name) You do not need to include the source. Obviously, for such a trivial Java class an IDE is not necessary. But I assume that if you want to code Java in MT4 then your code will be non-trivial.
Copy your JAR file to the same directory to which you copied the DLLs generated in the previous steps (probably the C:\Program Files\MetaTrader 4 directory).
If you haven't already, download IKVM, extract the zip file file (ikvmbin-0.40.0.1.zip) and put its BIN directory into your PATH. The BIN directory contains other DLLs that you will need to reference later.
Compile the .NET dll from your JAR at the command prompt with the following command:
ikvmc -target:library JavaClass.jar
This will create a file called JavaClass.dll. This DLL will have dependencies on the other DLL files from the ikvm\BIN folder.
Now we must modify the Visual Studio solution created in the previous exercise. Edit the CppStdcallInterfaceWrapper project, adding a Reference to the JavaClass.dll file that is in your C:\Program Files\MetaTrader 4 directory.
Next, add a Reference to IKVM.OpenJDK.Core.dll. This file is found in the IKVM\BIN directory. For both of the References, make sure that all of the Boolean Build Properties are set to True (Copy Local, Copy Dependencies, etc).
Open the CppStdcallInterfaceWrapper.cpp file. Comment out the dll import for the C# assembly and update the namespace as follows:
//#using "CSharpAssembly.dll"
using namespace com::corsairllc::mt4Import;
around line 34 or so, update the code that loads the C# class and change it to read
array^ char8ManArr =
JavaClass::Hello(nameManArr);
Now you can build the solution. On the first build you will see in the Ouput window the other IKVM dll files being copied into your project.
Copy all of the DLL files from the debug folder where the build files were placed, into the C:\Program Files\MetaTrader 4 directory. I have not tested whether the other IKVM DLL files are actually needed for this solution to work. Copying them will not hurt though.
Run the EA created in the CodeGuru tutorial, make sure to Allow DLL imports. You should get the following popup:
