如何在Pentaho的KTR中调用同Jar包内的Java类?
Hey there! I’ve worked through similar packaging scenarios with Pentaho and custom Java code, so let’s break down exactly how to get your HelloWorld class working with testTransformer.ktr:
Step 1: Verify Your JAR Structure First
This is make-or-break for class loading—ensure your JAR’s internal structure matches what you described perfectly:
MainJar.jar/ ├── testJob.kjb ├── testTransformer.ktr └── com/ └── mainTest/ └── HelloWorld.class
The HelloWorld class must live under the com.mainTest package directory tree, otherwise your import statement in the KTR will fail.
Step 2: Code the User Defined Java Class Component
Open your testTransformer.ktr in Spoon (Pentaho Design Studio), add the User Defined Java Class component, and paste in code like this (adjust to match your HelloWorld class’s actual methods):
// Import your custom class—match the package structure exactly import com.mainTest.HelloWorld; public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { // Grab the input row (standard Pentaho boilerplate) Object[] r = getRow(); if (r == null) { setOutputDone(); return false; } // Example 1: Call a static method from HelloWorld String staticMessage = HelloWorld.getStaticGreeting(); // Example 2: Instantiate HelloWorld and call an instance method // HelloWorld helloInstance = new HelloWorld(); // String customMessage = helloInstance.getCustomMessage("Pentaho User"); // Optional: Pass the result to an output field (add this field in the component's output settings first) r = createOutputRow(r, data.outputRowMeta.size()); get(Fields.Out, "java_class_output").setValue(r, staticMessage); // Send the row downstream to other steps putRow(data.outputRowMeta, r); return true; }
Quick Tips for This Code:
- Replace
getStaticGreeting()orgetCustomMessage()with the actual method names in yourHelloWorldclass. - If you need to pass input row data to your Java class, use
get(Fields.In, "input_field_name").getValue(r)to fetch values from the incoming row.
Step 3: Package & Test the JAR
When building your final JAR:
- Make sure all files (kjb, ktr, and compiled
HelloWorldclass) are included in the archive. No special manifest entries are required unless you’re creating an executable JAR—just maintaining the directory structure is enough.
To test:
- Copy your
MainJar.jarto Pentaho’sdata-integration/libdirectory (this lets Spoon load your custom class during design-time testing). - Run your
testTransformer.ktrin Spoon—you should see the output from yourHelloWorldclass in the specified output field.
Troubleshooting Common Hiccups
- ClassNotFound Exception: Double-check that the package path in your
importstatement matches the directory structure in the JAR. For example, if your class is incom/mainTest/HelloWorld.class, the import must beimport com.mainTest.HelloWorld;. - Method Not Found: Ensure your
HelloWorldmethods are markedpublic—Pentaho can’t access non-public methods from the component.
内容的提问来源于stack exchange,提问作者Vrajendra Singh Mandloi

