Below method works for me, assuming the jars from kettle/lib and kettle/libext are in your classpath
Code:
/**
* This method executes a job defined in a kjb file
*
* It demonstrates the following:
*
* - Loading a job definition from a kjb file
* - Setting named parameters for the job
* - Setting the log level of the job
* - Executing the job, waiting for it to finish
* - Examining the result of the job
*
* @param filename the file containing the job to execute (kjb file)
* @return the job that was executed, or null if there was an error
*/
public Job runJobFromFileSystem(String filename) {
try {
System.out.println("***************************************************************************************");
System.out.println("Attempting to run job "+filename+" from file system");
System.out.println("***************************************************************************************\n");
// Loading the job file from file system into the JobMeta object.
// The JobMeta object is the programmatic representation of a job definition.
JobMeta jobMeta = new JobMeta(filename, null);
// The next section reports on the declared parameters and sets them to arbitrary values
// for demonstration purposes
System.out.println("Attempting to read and set named parameters");
String[] declaredParameters = jobMeta.listParameters();
for (int i = 0; i < declaredParameters.length; i++) {
String parameterName = declaredParameters[i];
// determine the parameter description and default values for display purposes
String description = jobMeta.getParameterDescription(parameterName);
String defaultValue = jobMeta.getParameterDefault(parameterName);
// set the parameter value to an arbitrary string
String parameterValue = RandomStringUtils.randomAlphanumeric(10);
String output = "Setting parameter "+parameterName+" to \""+parameterValue+"\" [description: \""+description+"\", default: \""+defaultValue+"\"]";
System.out.println(output);
// assign the value to the parameter on the job
jobMeta.setParameterValue(parameterName, parameterValue);
}
// Creating a Job object which is the programmatic representation of a job
// A Job object can be executed, report success, etc.
Job job = new Job(null, jobMeta);
// adjust the log level
job.setLogLevel(LogLevel.MINIMAL);
System.out.println("\nStarting job");
// starting the job, which will execute asynchronously
job.start();
// waiting for the job to finish
job.waitUntilFinished();
// retrieve the result object, which captures the success of the job
Result result = job.getResult();
// report on the outcome of the job
String outcome = "\nJob "+ filename+" executed with result: "+result.getResult()+" and " +result.getNrErrors()+" errors\n";
System.out.println(outcome);
return job;
} catch (Exception e) {
// something went wrong, just log and return
e.printStackTrace();
return null;
}
}