简体   繁体   中英

Pentaho start a java program inside the same jar

We have an organization reports portal for which we use Pentaho. Now the catch is, we need to define one single java Jar in which Pentaho + java files + sql, all are there.

I'm able to execute SQL via Pentaho but I need to execute a class in the same jar via pentaho. This can be done via user Defined Java Class. But how to do it? How to directly import java class in the KTR --> UserDefinedJavaClass.

Folder Structure in jar:

MainJar
|-testJob.kjb
|-testTransformer.ktr
|-com.mainTest.HelloWorld.class

How to call HelloWorld.class from test.ktr?

I haven't been able to get it working with User Defined Java Class step but using the Javascript step instead. You need to copy the JAR to pentaho /lib or /libext folder first. Then in the modified javascript step I just called the class. Here is my step XML

<entry>
  <name>JavaScript</name>
  <description />
  <type>EVAL</type>
  <script>
   var filename=parent_job.getVariable("filename","");
   var filenameClean=filename+"_clean";
   package.CsvCleaner(filename,filenameClean);
  </script>
  <parallel>N</parallel>
  <draw>Y</draw>
  <nr>0</nr>
  <xloc>800</xloc>
  <yloc>1008</yloc>
</entry>

The important part is package.CsvCleaner(filename,filenameClean); actually invokes the constructor of the class with the two parameters and I have added my code in the constructor. You can also call methods and stuff.

I guess the User Defined Java is called in a similar way -add the jar to /lib import the class and use it.

I have one advise though. In my opinion it is better to leave the JAR with the actual ETL code (and not making it part of pentaho by adding it to /lib or /libext) and execute it using a shell command - something like java -jar MyProg.jar Otherwise you will have strange problems:

  • having an older version of the Jar in the pentaho installation folder
  • harder installation because that jar is not where the kjb and kjt files are
  • if pentaho is installed with a different user than the one deploying the ETL he might not have rights to access /lib and /libext folders
  • If you have more complicated java code and logic it is better to keep it in the jar instead of in a transformation (having java code in two places is a maintenance hell). So adding the jar to pentaho gives you no benefit over just calling java -jar MyJar.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM