Using Java in adapters
To learn more about adapters in general, start with the Adapters Overview tutorial.
The Java code is an extension of the adapter function and not a replacement.
Adding custom Java classes to your project
- To use an existing Java library, add the JAR file to the server\lib folder of your MobileFirst project.
After the adapter is built and deployed, this JAR file is automatically deployed to MobileFirst Server.
- To add custom Java code to your project, right-click the server/java folder in your Worklight project and add a Java class file. Name it Calculator.java.
Important: The package name must start with either com, org, or net.
- Add this file to a package. In this sample, the
This package name can be interpreted as folders: java\com\sample\customcode
- Add methods to your Calculator.java class.
Here is an example of a static method (does not require a new instance) and of an instance method.
- If your Java code has additional dependencies, put the required JAR files in the server\lib folder of your MobileFirst project.
Invoking custom Java classes from the adapter
Invoke the static Java method as shown, and use the full class name to reference it directly.
To use the instance method, create a class instance and invoke the instance method from it.
Click to download the Studio project.▲