Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java code to access windows machine from unix machine

992608Feb 28 2014 — edited Mar 3 2014

Hi All,

I have a requirement where I have to send a file from a local system to unix box(present on client side) using java code.I have developed a code that is successfully sending the file from local system to client side unix box(I am connecting to client side unix box using VPN) provided I run the code in my eclipse IDE present in local system.But when I am running the same code in the unix box it is throwing null pointer exception.Might be the unix system is not recognising the local system.Please find the code.

package abc;

import java.io.File;

import java.io.FileInputStream;

import java.io.FilenameFilter;

import java.io.IOException;

import java.util.Properties;

import com.jcraft.jsch.Channel;

import com.jcraft.jsch.ChannelSftp;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.JSchException;

import com.jcraft.jsch.Session;

import com.jcraft.jsch.SftpException;

public class TransferFile {

    private static Properties props=new Properties();

    public static void main(String args[]) throws JSchException, SftpException, IOException

    {

        sftpConnection();

    }

    public static void sftpConnection() throws JSchException, SftpException, IOException {

        System.out.println("Inside sftpConnection method");

        JSch jsch = new JSch();

        Session session = null;

        Channel channel = null;

        props.loadFromXML(new FileInputStream("Config.xml"));

        String user = props.getProperty("server.username");

        String host = props.getProperty("server.hostname") ; //unix box host name

        Integer port =Integer.parseInt(props.getProperty("server.port")); // unix box port

        String password =props.getProperty("server.password"); //unix box password

        String watchFolder = props.getProperty("localmachine.watchFolder"); //local machine folder from where I have to fetch the file(D:\textFiles)

        String outputDir = props.getProperty("server.outputDir"); //unix box output folder (/home/oracle/)

        String filemask = props.getProperty("filemask");  // file mask(.txt)

        ChannelSftp sftpChannel = null;

        try

        {

            session = jsch.getSession(user, host, port);

            System.out.println("llllllll");

            session.setConfig("StrictHostKeyChecking", "no");

            session.setPassword(password);

            System.out.println("fffffffffffff");

            session.setConfig("PreferredAuthentications","publickey,keyboard-interactive,password");

            session.connect();

            System.out.println("Connection Successfull");

            channel = session.openChannel("sftp");

            channel.connect();

            System.out.println("Channel Connection Succesfull");

            sftpChannel = (ChannelSftp)channel;

            System.out.println("aaaaa");

            File[] files = findFile(watchFolder, filemask);

            for(File file : files)

            {

                putFile(file, sftpChannel, outputDir);           

            }                

        }

        finally

        {

            if (sftpChannel != null) {

            sftpChannel.exit();

            }

            session.disconnect();

        }

    }

    public static void putFile(File file, ChannelSftp sftpChannel, String outputDir) throws SftpException, IOException

    {

        FileInputStream fis = null;

        try

        {

            sftpChannel.cd(outputDir);

            fis = new FileInputStream(file);

            sftpChannel.put(fis, file.getName());

            fis.close();

        }

        finally

        {

        }

    }

    public static File[] findFile(String dirName, final String mask)

    {

        System.out.println("Inside findFile method");

        File dir = new File(dirName);

        return dir.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String filename)

            {

                System.out.println("eeeeeee");

                return filename.endsWith(mask);

            }

        }

                );

    }

}

Error on unix box which I am getting is :-

Inside sftpConnection method

llllllll

fffffffffffff

Connection Successfull

Channel Connection Succesfull

aaaaa

Inside findFile method

Exception in thread "main" java.lang.NullPointerException

        at abc.TranferFile.sftpConnection(TranferFile.java:58)

        at abc.TranferFile.main(TranferFile.java:21)

Please help urgently....................

This post has been answered by jashburn on Mar 2 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 31 2014
Added on Feb 28 2014
4 comments
2,567 views