title: JSch SFTP put to IBM z/OS fails :failed to write file; nested exception is 3: Permission denied
body: <p>I am trying to put file via SFTP to mainframe using JSch.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public void uploadFile(String contents) throws Exception {
      JSch.setLogger(new JSCHLogger());
      connectIfDisconected();
      LOGGER.info("Channel isConnected=: " + channel.isConnected());
      OutputStream os = channel.put("//!DTS4.UP.G5TB4.S60301");
      os.write(contents.getBytes());
      os.flush();
      os.close();
  }
  
  protected void connectIfDisconected() throws JSchException {
      if (!session.isConnected()) {
          LOGGER.info(LogEvent.create(".connectIfDisconected()", "Reconnecting session and channel..."));
          session.connect();
          if (channel != null) {
              channel.exit();
          }
          channel = (ChannelSftp) session.openChannel("sftp");
      } else if (!channel.isConnected()) {
          LOGGER.info(LogEvent.create(".connectIfDisconected()", "Reconnecting channel..."));
          channel.connect();
      }
  }
  </code></pre>
  
  <p>Here is the session configuration:</p>
  
  <pre><code>session = jsch.getSession(username, host, port);
  Properties config = getProperties(host, port);
  session.setConfig(config);
  ...
  ...
  protected Properties getProperties(String host, int port) throws JSchException {
      Properties config = new Properties();
      config.put("StrictHostKeyChecking", strictHostChecking ? "yes" : "no");
      config.put("kex", "diffie-hellman-group-exchange-sha1");
      config.put("PreferredAuthentications", "password");
      return config;
  }
  </code></pre>
  
  <p>Session is connected fine! </p>
  
  <p>While I'm trying to put file I'm getting the following exception on line</p>
  
  <pre><code>OutputStream os = channel.put(targetFile);
  </code></pre>
  
  <pre class="lang-none prettyprint-override"><code>com.three60t.tex.app.exceptions.business.BusinessException: Error while delivering to Client communicator : Permission denied
          at com.three60t.integration.posttrade.utils.DefaultReporter.reportError(DefaultReporter.java:68)
          at com.three60t.integration.posttrade.transformationService.InternalTransformationService.reportError(InternalTransformationService.java:439)
          at com.three60t.integration.posttrade.transformationService.InternalTransformationService.deliver(InternalTransformationService.java:378)
          at com.three60t.integration.posttrade.transformationService.InternalTransformationService.doDeliver(InternalTransformationService.java:314)
          at com.three60t.integration.posttrade.transformationService.InternalTransformationService.transform(InternalTransformationService.java:305)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:497)
          at com.three60t.tex.communication.message.handler.MessageDispatcher.dispatchToObject(MessageDispatcher.java:722)
          at com.three60t.tex.communication.message.handler.MessageDispatcher.doExecuteObject(MessageDispatcher.java:543)
          at com.three60t.tex.communication.message.handler.MessageDispatcher.access$200(MessageDispatcher.java:77)
          at com.three60t.tex.communication.message.handler.MessageDispatcher$1.run(MessageDispatcher.java:264)
          at com.three60t.tex.communication.message.handler.MessageDispatcher$3.run(MessageDispatcher.java:444)
          at com.three60t.concurrent.executor.v2.RunnableWithTimings.run(RunnableWithTimings.java:81)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
          at java.lang.Thread.run(Thread.java:745)
  Caused by: 3: Permission denied
          at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
          at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:753)
          at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:700)
          at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:694)
          at com.three60t.integration.tradeimporters.sftp.DtccSftpClient.uploadFile(DtccSftpClient.java:34)
          at com.three60t.integration.tradeimporters.sftp.SFTPTransportDtcc.upload(SFTPTransportDtcc.java:41)
          at com.three60t.framework.communication.integration.SFTPTransport.upload(SFTPTransport.java:148)
          at com.three60t.integration.tradeimporters.sftp.SFTPTransportDtcc.upload(SFTPTransportDtcc.java:25)
          at com.three60t.integration.tradeimporters.sftp.SFTPTransportDtcc.deliver(SFTPTransportDtcc.java:55)
          at com.three60t.integration.posttrade.transformationService.InternalTransformationService.deliver(InternalTransformationService.java:362)
          ... 15 more
  </code></pre>
  
  <p>When I'm trying the same using terminal everything works fine!</p>
  
  <pre class="lang-none prettyprint-override"><code>sftp XXXX@fdmoverspse.dtcc.com
  Connecting to fdmoverspse.dtcc.com...
  XXXX@fdmoverspse.dtcc.com's password: 
  sftp&gt; put myfile.xml //!DTS4.UP.G5TB4.S60301
  Uploading myfile.xml to //!DTS4.UP.G5TB4.S60301
  myfile.xml       100% 7901     7.7KB/s   00:00   
  sftp&gt;
  </code></pre>
  
  <p>Any ideas how to fix?</p>