-1

this is the error i got

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

this is the code I'm working on

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]);
}   }

Thank you in advance for any help. I'm not looking to have this done for me, I'm just stuck and need help finding my way.

and how to make this possible to transfer file from my server to other server?

Ken Kaneki
  • 127
  • 4
  • 14

1 Answers1

-1

You need to supply the program arguments for it to run.

If you are using some ide like eclipse.

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

Incase you are using from cmd use something like:

java TrfFiles2Dev you-file-name-here.txt 

You can also take the fileName while program is running using Scanner class. Something like:

Scanner scn = new Scanner(System.in);
String fileName = scn.nextLine();
TrfFiles2Dev trfFiles2Dev = new TrfFiles2Dev();
trfFiles2Dev.send(args[0]);
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Shanu Gupta
  • 3,333
  • 2
  • 15
  • 27
  • does the file have to be in one directory with this java file to send, cause i get the exception found while transfer the response? – Ken Kaneki Apr 25 '18 at 03:46
  • i used terminal and how i can send all my file in specific directory ? thanks in advanced – Ken Kaneki Apr 25 '18 at 03:47
  • File has to be on classpath. But if you are unsure of that, you can use absolute path of file like `C:\\results\\you-file-name.txt` – Shanu Gupta Apr 25 '18 at 03:49
  • Please see https://stackoverflow.com/questions/1693020/how-to-specify-filepath-in-java to get more details how to give the file path. – Shanu Gupta Apr 25 '18 at 03:52
  • But primary reason for `ArrayIndexOutOfBoundsException` is lack of program arguements in your case. – Shanu Gupta Apr 25 '18 at 03:53
  • I'm not sure what remains unclear from my code snippets. Make sure you read https://meta.stackexchange.com/questions/19665/the-help-vampire-problem – Shanu Gupta Apr 25 '18 at 03:56