Call Java from Magic & uniPaaS
The information on this page is intended for those of you who are overall happy with developing and deploying in Magic/uniPaaS but sometimes wish you could take advantage of some of the powerful classes and methods available in the standard Java distribution. Using some of these classes can avoid the need to re-invent the wheel in Magic/uniPaaS and also avoid the need to search for, trust and bundle a third party ActiveX, since the Java class you need is most likely already on your and your client's PC.
Say you wanted to filter out the US postal code in free format address strings such as:
- "1 Main Street at Hollywood CA 90210 USA"
- "1 Main Street on Beverly Hills California 90210 United States"
- "1 Main Street Beverly Hills CA90210"
- "12345 Main Street Beverly Hills California 90210 United States"
- "12345 Main Street, Beverly Hills, California 90210-1107, United States"
- "12345 Main Street Beverly Hills California 90210 1107 United States"
To our human eyes it easy to see what the postal code is. It would however be quite a challenge to write a Magic routine that would return the 5 digit (and if available, the 9 digit code).
Ideally you would want to have a function that takes the search string and a regular expression representing a search pattern as its two parameters:
postalCode = findLastMatch("#####(.####)?", stringToBeSearched);
Since Magic V9 you have Magic functions such as JCALL and JCALLSTATIC that make it very easy to call Java methods. The best news is that these Java methods do not need any special code to make them work with Magic. Creating and calling a DLL with C or C++, was a much bigger chore.
In many cases you will not even need to write your own Java class or method, as there are hundreds of existing classes and thousands of methods available to anyone as part of the standard Java distribution.
Here is the list of classes that is available as part of Java 6:
Browsing through this list can be overwhelming though. In our case we just want to write a pattern matching function. The first step is to determine if Java has classes and methods that provide assistance for such a challenge. Just google for "Java String Pattern" and you will find the documentation for the Pattern class.
You see that this class allows us to call a method that takes a regular expression to describe the pattern we are looking for, and the string we want to search. The documentation provides the following example.
boolean b = Pattern.matches("a*b", "aaaaab"); // this would return true
The documentation of the method "matches" indicates it is a static method. When a Java method is static, you do not need to create an object of the class first to call the method (so there is no need to use Magic's JCREATE function to first create a Pattern object). You simply use the JCALLSTATIC method to call this method from Magic without the need for any additional code in Magic.
While the standard method "matches" is powerful enough to tell us if we have found a postal code, we want to return the last match. In order to do this we wrote a small Java class called UDF with a method called findLastMatch.
Here is what the call from Magic looks like:
JCallStatic ('M2J.UDF.findLastMatch', '(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;',A,B)
This call tells Magic that the Java method findLastMatch is in package M2J, class UDF and that the Java method expects two String parameters and will return a String parameter. Parameter A is the pattern we are looking for and parameter B will contain the string we are about to search.
Sample code you can download
An export of the Magic V9.4sp7 program calling this method, an export of the uniPaaS 1.9g project that contains this sample and a few other samples, their Java source and the M2J executable jar that contains the compiled class can be downloaded here.
Just copy the M2J.jar in a directory and include the jar as part of the CLASSPATH in your Magic.ini. For example if your CLASSPATH is set to:
and you copied the M2J.jar into C:\MAGIC then set your CLASSPATH to
You can have multiple java environments on one machine. Magic requires you to use a 32-bit version of Java. In your Magic.ini make sure to point your JAVA_HOME to a 32 bit Java installation directory. For example:
You can comment out the JVM_PATH= option in your Magic.ini:
Use best of both worlds. If you find that a simple task is not simple in Magic or uniPaaS, chances are there is a standard Java class and method available as part of the standard Java distribution that could save you a lot of time. As of Magic V9 it is easy to call such a Java method and use it as part of your Magic application.
We hope this helps you get started writing your own methods.
Feel free to contact us if you need assistance or if you would rather outsource writing new methods to us.