1

I want to Access file through my java program remotely from Linux sever.I have write this code but its not working.

public static void main(String arg[]) throws FileNotFoundException
    {
        JSch jsch = new JSch();
        Session session = null;
        BufferedReader br = null;
        try {
            session = jsch.getSession("root", "IPServer", 22);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword("Password");
            session.connect();
            System.out.println("Server is connect");
            try
            {
                String command = "scp -f /var/log/callrec/core1.log";
                Channel channel = session.openChannel("exec");
                ((ChannelExec)channel).setCommand(command);

                OutputStream out = channel.getOutputStream();
                InputStream in = (InputStream) channel.getInputStream();

                channel.connect();

                br = new BufferedReader(new InputStreamReader(in));

                StringBuilder sb = new StringBuilder();

                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }

                System.out.println("\nDone!");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            session.disconnect();
        } catch (JSchException e) {
            e.printStackTrace();  
    //    } catch (SftpException e) {
     //       e.printStackTrace();
        }
    }

My program did not throw any error, i can see "server is connect" message on my console and then its hanged and not give any other output and it didn't terminate....

1 Answer 1

1

First check if you can connect via ssh with same parameters from shell to this server. You will eliminate networking problems variant. For example linux server may deny remote root login.

Then once your program hangs, log in to this linux server and run who command to see, if your java program was successful with login. Alternatively you can run ps -afx command to see, if scp command is running. Or better execute touch /var/tmp/java-test command from Java instead of scp. Then ls will tell you, if you succeeded with executing remote command from java on this server.

I hope this will help you to find the cause.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.