Differences

This shows you the differences between two versions of the page.

Link to this comparison view

documentation:tutorial:usecustomjavapackage [2018/10/19 17:23] (current)
sfroger created
Line 1: Line 1:
 +====== Use custom java package ======
 +
 +===== Presentation =====
 +
 +When scripting in LSC (see [[documentation:​latest:​scripting:​start]]),​ you can import a java package and use its java classes within LSC JS engine. All you need is to add your jar file and its dependencies into LSC lib folder and import your package at the beginning of your script.
 +
 +This tutorial shows how to set up a "hello world" type call.
 +
 +===== Create jar file =====
 +   * In a folder, create folder path com/myjar
 +   * Add a file Utils.java in myjar folder, containing :
 +<​code>​
 +package com.myjar;
 +public class Utils {
 + public static String helloWorld() {
 + return "hello world";​
 + }
 +}
 +</​code>​
 +   * Compile : ''​javac com/​myjar/​Utils.java''​
 +   * Package : ''​jar cfv myjar.jar com.myjar.Utils com/​myjar/​Utils.class''​
 +
 +===== Adapt LSC to see and call your package class =====
 +   * Place myjar.jar file into your ''​%lsc_home%/​lib''​ folder
 +   * Add this in your lsc.xml :
 +<​code>​
 +        <​dataset>​
 +           <​name>​sn</​name>​
 +           <​forceValues>​
 +            <​string>​js:​importPackage(com.myjar);​Utils.helloWorld();</​string>​
 +           </​forceValues>​
 +        </​dataset>​
 +</​code>​
 +   * **Result** : The sn attributes of users are all populated with string "hello world"​.
 +
 +
 +
 +
 +
 +
 +