【问题标题】:getting error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 [duplicate]在线程“main”java.lang.ArrayIndexOutOfBoundsException中出现错误异常:0 [重复]
【发布时间】:2018-04-25 03:23:11
【问题描述】:

这是我遇到的错误

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0  at TrfFiles2Dev.main(TrfFiles2Dev.java:55

这是我正在处理的代码

public  void send (String fileName) {
    String SFTPHOST = "";
    int SFTPPORT = 22;
    String SFTPUSER = "";
    String SFTPPASS = "";
    String SFTPWORKINGDIR = "";
    Logger log = Logger.getLogger(TrfFiles2Dev.class.getName() );
    Session session = null;
    Channel channel = null;
    ChannelSftp channelSftp = null;
    System.out.println("preparing the host information for sftp.");
    try {
        JSch jsch = new JSch();
        session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
        session.setPassword(SFTPPASS);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        System.out.println("Host connected.");
        channel = session.openChannel("sftp");
        channel.connect();
        System.out.println("sftp channel opened and connected.");
        channelSftp = (ChannelSftp) channel;
        channelSftp.cd(SFTPWORKINGDIR);
        File f = new File(fileName);
        channelSftp.put(new FileInputStream(f), f.getName());
        log.info("File transfered successfully to host.");
    } catch (Exception ex) {
         System.out.println("Exception found while tranfer the response.");
    }
    finally{

        channelSftp.exit();
        System.out.println("sftp Channel exited.");
        channel.disconnect();
        System.out.println("Channel disconnected.");
        session.disconnect();
        System.out.println("Host Session disconnected.");
    }
}   
public static void main(String[] args) {
    TrfFiles2Dev trfFiles2Dev = new TrfFiles2Dev();
    trfFiles2Dev.send(args[0]);
}   }

提前感谢您的帮助。我不想为我完成这件事,我只是卡住了,需要帮助找到我的方式。

以及如何将文件从我的服务器传输到其他服务器?

【问题讨论】:

  • TrfFiles2Dev 中的第 55 行是什么
  • 哪一行出错了?
  • 你能分享这门课吗?
  • 基本上这个错误告诉你在第 55 行访问的数组在你尝试使用的数组大小内没有索引。
  • 可能 55 是 trfFiles2Dev.send(args[0]);,这意味着你在运行程序时没有指定任何命令行参数

标签: java jsch


【解决方案1】:

您需要提供program arguments 才能运行。

如果你正在使用像 eclipse 这样的 ide。

Goto run-> run configurations-> give arguments-> then run

如果您从 cmd 使用,请使用类似:

java TrfFiles2Dev you-file-name-here.txt 

您还可以在程序运行时使用fileName 类使用Scanner。比如:

Scanner scn = new Scanner(System.in);
String fileName = scn.nextLine();
TrfFiles2Dev trfFiles2Dev = new TrfFiles2Dev();
trfFiles2Dev.send(args[0]);

【讨论】:

  • 文件是否必须与此 java 文件位于一个目录中才能发送,因为我在传输响应时发现异常?
  • 我使用了终端,如何将我的所有文件发送到特定目录?感谢先进
  • 文件必须在类路径中。但是如果你不确定,你可以使用文件的绝对路径,比如C:\\results\\you-file-name.txt
  • 请参阅stackoverflow.com/questions/1693020/… 以获取更多详细信息如何提供文件路径。
  • ArrayIndexOutOfBoundsException 的主要原因是您的案例缺乏程序争论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 2020-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多