Creating a Custom JAR
In this example we will create a Custom JAR containing a simple Java class that converts a string to camel caps through a static method. This class can be used in one or more Java functions in a mapping using the Custom JAR. In this example, we use the Eclipse program that hosts the mapper to create the Custom JAR file. You can also use another Eclipse installation or other Java development environment.
-
Start the mapper.
The mapper is a plugin to Eclipse. To develop the custom class we will use the standard Java perspective in Eclipse, not the Mapping Development perspective.
-
Open the Java perspective.
Select Window > Open Perspective > Java.
- Right-click in the Package Explorer view, select New > Java Project.
-
Specify a project name, for example
MyCustomJAR
.The name you specify must not be used by a mapping project in the same workspace. Keep all default settings for the Java project.
A new Java project is created, it contains a source folder, src, and a JRE System Library. The JRE System Library is included in the mapper installation, no external JRE or JDK installation is necessary.
-
Select the Java project.
Select Open Project > Properties.
To be able to show the Javadoc in the mapper, the Java source file must be written using the character encoding UTF-16.
Note: Do this before you create any .java files in the project, otherwise existing .java files may become unreadable.- In the Resource group, set Text file encoding to UTF-16.
- Click OK.
- Right-click on the src folder for the project, select New > Package.
-
Specify a package name, for example cust.
An empty package is created in the src folder.
- Right-click on the package, select New > Class.
-
Specify a custom class a name, for example MyCustomClass.
The class must be public. A code skeleton for the class is created in the package, and opened in the Java editor.
-
Copy this Java code into the Java editor, replacing the generated skeleton.
If you use another package or class name, change the package or class name so that the Java code will compile.
package cust; /** * This is a custom class. * * @author My Name * @since 1.0 */ public class MyCustomClass { /** * Converts a string to camel caps. * * <p><b>Example:</b><br> * The input string {@code "Mary had a little lamb"} gives * the output string {@code "MaryHadALittleLamb"}.</p> * * @param s String to convert. * @return Converted string in camel caps. */ public static String toCamelCaps(String s) { if (s == null) { return null; } StringBuilder sb = new StringBuilder(s.length()); boolean toUpperCase = false; for (char c : s.toCharArray()) { if (c == ' ') { toUpperCase = true; } else if (toUpperCase) { sb.append(Character.toUpperCase(c)); toUpperCase = false; } else { sb.append(c); } } return sb.toString(); } }
- Right-click on the Java project, select Export.
- Select Java > JAR file and click Next.
-
Resources for the current project is pre-selected.
Use these guidelines:
-
You may clear the .classpath and .project files options. These are not used by the mapper.
-
Select to export generated class files and resources. This is selected as default.
-
Select to export Java source files and resources if you want Javadoc for your custom class in the mapper.
-
Select a JAR file name, for example
MyCustomJAR.jar
. -
Select to compress the contents of the JAR file. This is selected as default.
-
-
Click Finish.
Your Custom JAR file is now created and can be used in any mapping.