You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pentaho的KTR中调用同Jar包内的Java类?

How to Call Embedded Java Class from Pentaho KTR's User Defined Java Class Component

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() or getCustomMessage() with the actual method names in your HelloWorld class.
  • 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 HelloWorld class) 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:

  1. Copy your MainJar.jar to Pentaho’s data-integration/lib directory (this lets Spoon load your custom class during design-time testing).
  2. Run your testTransformer.ktr in Spoon—you should see the output from your HelloWorld class in the specified output field.

Troubleshooting Common Hiccups

  • ClassNotFound Exception: Double-check that the package path in your import statement matches the directory structure in the JAR. For example, if your class is in com/mainTest/HelloWorld.class, the import must be import com.mainTest.HelloWorld;.
  • Method Not Found: Ensure your HelloWorld methods are marked public—Pentaho can’t access non-public methods from the component.

内容的提问来源于stack exchange,提问作者Vrajendra Singh Mandloi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:18:37