1024programmer Java Customize the jar package for JavaScript use through Kettle

Customize the jar package for JavaScript use through Kettle

This article mainly introduces the custom jar package for Javascript to be used through Kettle. The article introduces it in great detail through sample code. It has certain reference learning value for everyone’s study or work. Friends who need it can refer to it

We all know that Kettle is developed in Java language, and java class methods can be called directly in Javascript. So sometimes, we can customize some methods for use by Javascript.

1. Create tool classes in java projects

In the project, create a utils tool class, such as a method to calculate the total page number. The code is as follows:

 public class PaginationUtils {

  /**
   * Calculate the total page number
   * @param totalRecords total number of records
   * @param pageSize paging size
   * @return total page number
   */
  public static int totalPage(String totalRecords,String pageSize){
   int totalPage=0;
   try{
    BigDecimal records=new BigDecimal(totalRecords);
    BigDecimal size=new BigDecimal(pageSize);
    BigDecimal _tmp=records.add(size).subtract(new BigDecimal(1));
    BigDecimal _tp=_tmp.divide(size).setScale(0,BigDecimal.ROUND_HALF_UP);
    totalPage=_tp.intValue();
   }catch (Exception e){
    //error
   }
   return totalPage;
  }
 }

2. Deployment and packaging

After the tool class method is developed, it can be packaged through the mvn clean package -DskipTests command. A jar file will be generated in the target directory. This jar package needs to be placed in the lib directory of kettle. As shown below:

3. Write Javascript scripts

Restart Kettle, create a new Javascript script, and the js code for calculating the total page number is as follows:

 //Calculate total page number
 var totalPage=com.study.spring.Utils.PaginationUtils.totalPage(countBySql,pageSize);

In fact, it means declaring java classes + methods in js code.

But here comes the problem. When kettle runs this Javascript script, it prompts the following error:

 Cannot compile Javascript: org.mozilla.Javascript.EcmaError: TypeError: Cannot call property totalPage in object [JavaPackage com.study.spring.Utils.PaginationUtils]. It is not a function, it is "object". (#  22)

According to the error message, in fact, the relevant class method is still not found, just like the jar package is not placed…

4. FAQ

What exactly is going on with the above error? I’m weird too. So I used the compression tool and looked at other jar package structures in the lib directory, and found that they all looked like this:

The directory of the jar package I just created looks like this:

The com directory is not in the root path of the jar package, so naturally the method cannot be accessed.

Okay, the cause of the problem has been found, and then let’s talk about the solution:

Add in pom.xml file:

 
   
     
       org.springframework.boot
       spring-boot-maven-plugin
       
         true
       
     
   
 

The rendering after adding the pom file is as follows:

The internal structure of the regenerated jar package is:

Success! The com directory is already in the root path of the jar package.

Replace the jar package into the lib directory of kettle, restart Kettle, and call the custom jar package through Javascripts again successfully!

The above is the entire content of this article. I hope it will be helpful to everyone’s study and I hope you will support me a lot.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/697296

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索