{"record":{"id":"04731d95cc440aaf","repo":"pentaho/pentaho-kettle","slug":"unable-to-write-target-file-ktr-after-injection-to-file","errorCode":null,"errorMessage":"Unable to write target file (ktr after injection) to file '{targetFilePath}'","messagePattern":"Unable to write target file \\(ktr after injection\\) to file '(.+?)'","errorType":"exception","errorClass":"KettleException","httpStatus":null,"severity":"error","filePath":"plugins/meta-inject/impl/src/main/java/org/pentaho/di/trans/steps/metainject/MetaInject.java","lineNumber":289,"sourceCode":"\n    OutputStream os = null;\n    try {\n      // don't clear all the clone's data before copying from the source object\n      TransMeta generatedTransMeta = (TransMeta) data.transMeta.realClone( false );\n      File injectedKtrFile = new File( targetFilePath );\n\n      if ( injectedKtrFile == null ) {\n        throw new IOException();\n      } else {\n        String transName = injectedKtrFile.getName().replace( \".ktr\", \"\" );\n        generatedTransMeta.setName( transName ); // set transname on injectedtrans to be same as filename w/o extension\n      }\n\n      os = KettleVFS.getInstance( getTransMeta().getBowl() ).getOutputStream( targetFilePath, false );\n      os.write( XMLHandler.getXMLHeader().getBytes( Const.XML_ENCODING ) );\n      os.write( generatedTransMeta.getXML().getBytes( Const.XML_ENCODING ) );\n    } catch ( IOException e ) {\n      throw new KettleException( \"Unable to write target file (ktr after injection) to file '\"\n        + targetFilePath + \"'\", e );\n    } finally {\n      if ( os != null ) {\n        try {\n          os.close();\n        } catch ( Exception e ) {\n          throw new KettleException( e );\n        }\n      }\n    }\n  }\n\n  /**\n   * Writes the generated meta injection transformation to the repository. It is assumed that the repository\n   * exists (user is connected).\n   * @param targetFilePath the repo path to which to save the generated injection ktr\n   * @throws KettleException\n   */","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/plugins/meta-inject/impl/src/main/java/org/pentaho/di/trans/steps/metainject/MetaInject.java#L271-L307","documentation":"Thrown by MetaInject.writeInjectedKtrToFs() (called from writeInjectedKtr) when writing the generated, injected .ktr XML to the target file throws an IOException. The step opens an output stream via KettleVFS, writes the XML header and the generatedTransMeta XML, and wraps any IOException in this KettleException. The injected transformation file is not produced when this happens.","triggerScenarios":"The OutputStream obtained from KettleVFS.getOutputStream(targetFilePath, false) cannot be written to: the target directory does not exist, the file is locked, disk is full, or filesystem permissions deny writing.","commonSituations":"'Show injected transformation' / debug output path pointing to a non-existent or read-only directory; target file open in another program (Windows lock); VFS path typo (bad scheme); insufficient user permissions; disk quota exceeded.","solutions":["Check the wrapped IOException in the log for the concrete filesystem cause","Verify the target directory exists and the process user has write permission to targetFilePath","Ensure the target .ktr is not locked/open elsewhere and there is free disk space","Correct or clear the 'target file' option in the Meta Inject dialog if it points to an invalid VFS URI"],"exampleFix":"// before: non-existent output dir\nmeta.setTargetFilePath('file:///tmp/missing-dir/injected.ktr');\n// after: ensure dir exists first\nnew java.io.File('/tmp/out').mkdirs();\nmeta.setTargetFilePath('file:///tmp/out/injected.ktr');","handlingStrategy":"try-catch","validationCode":"// Pre-check target writeability\njava.io.File dir = new java.io.File('/tmp/out');\nif (!dir.isDirectory()) dir.mkdirs();\njava.io.File f = new java.io.File(dir, 'injected.ktr');\nif (!f.canWrite() && f.exists()) throw new IllegalStateException('Target file not writable');","typeGuard":"boolean canWriteTarget(String vfsPath) {\n  try (java.io.OutputStream os = new java.io.FileOutputStream(vfsPath.replaceAll('^file://',''), true)) {\n    return os != null;\n  } catch (IOException e) { return false; }\n}","tryCatchPattern":"try {\n  // run Meta Inject with target file set\n} catch (KettleException e) {\n  if (e.getMessage().startsWith('Unable to write target file')) {\n    log.error('Check dir exists, permissions, disk space, file lock: ' + e.getCause(), e);\n  } else throw e;\n}","preventionTips":["Ensure the target directory exists before execution","On Windows, close the target .ktr in editors before re-running","Use simple absolute paths to avoid VFS scheme typos","Monitor disk quota on the output volume"],"tags":["io","filesystem","metadata-injection"],"backgroundTag":"file-write-failed","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}