Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Error connecting to Unix and running script from Java code

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error connecting to Unix and running script from Java code

    I get the following error when trying to run code. Please help.

    java.lang.NullPointerException
    at conntecttoDB8.SSHCommandExecutor.main(SSHCommandEx ecutor.java:26)


    package conntecttoDB8;
    import java.io.InputStream;
     
    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.Session;
     
     
    public class SSHCommandExecutor {
     
        /**
         * @param args
         */
        public static void main(String[] args) {
        	//String host="xxxxxxxxxxxxxx";
        	String host="xxxxxxxxxxxxxxx";
    		String user="xxxxxxxxxxxxx";
    		String password="xxxxxxxxxxxx";
    		String command1="ls -ltr";
            try{
     
                java.util.Properties config = new java.util.Properties(); 
                config.put("StrictHostKeyChecking", "no");
                JSch jsch = new JSch();
                Session session=jsch.getSession(user, host, 22);
                session.setPassword(password);
                session.setConfig(config);
                session.connect();
                System.out.println("Connected"); 
                Channel channel=session.openChannel("exec");
                channel.setCommand(command1);
                channel.setInputStream(null);
                channel.setInputStream(System.err);
     
                InputStream in=channel.getInputStream();
                channel.connect();
                byte[] tmp=new byte[1024];
                while(true){
                  while(in.available()>0){
                    int i=in.read(tmp, 0, 1024);
                    if(i<0)break;
                    System.out.print(new String(tmp, 0, i));
                  }
                  if(channel.isClosed()){
                    System.out.println("exit-status: "+channel.getExitStatus());
                    break;
                  }
                  try{Thread.sleep(1000);}catch(Exception ee){}
                }
                channel.disconnect();
                session.disconnect();
                System.out.println("DONE");
            }catch(Exception e){
                e.printStackTrace();
            } finally {
    		}
     
        }
     
    }
     
    package com.jcraft.jsch;
     
    import java.util.Properties;
     
    public class Session {
     
    	public void connect() {
     
     
    	}
     
    	public Channel openChannel(String string) {
    		return null;
     
     
    	}
     
    	public void setConfig(Properties config) {
     
     
    	}
     
    	public void setPassword(String password) {
     
    	}
     
    	public void disconnect() {
     
    	}
     
    }
     
    package com.jcraft.jsch;
     
    import java.io.PrintStream;
     
    public class ChannelExec {
     
    	public void setCommand(String command1) {
     
     
    	}
     
    	public void setErrStream(PrintStream err) {
     
     
    	}
     
    }
     
    package com.jcraft.jsch;
     
    import java.io.InputStream;
     
    public class Channel {
     
    	public void setInputStream(Object object) {
     
     
    	}
     
    	public InputStream getInputStream() {
     
    		return null;
    	}
     
    	public void connect() {
     
     
    	}
     
    	public boolean isClosed() {
     
    		return false;
    	}
     
    	public String getExitStatus() {
     
    		return null;
    	}
     
    	public void disconnect() {
     
     
    	}
     
    	public void setCommand(String command1) {
     
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Error connecting to Unix and running script from Java code

    java.lang.NullPointerException
    at conntecttoDB8.SSHCommandExecutor.main(SSHCommandEx ecutor.java:26)
    There was a null value at line 26. Look at line 26 to find what was null. Then backtrack in the code to see why it was null.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error connecting to Unix and running script from Java code

    Thanks,

    I did ask suggested and all variables prior have values.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Error connecting to Unix and running script from Java code

    What statement is on line 26?

    That error usually means there is a null value. How did you check the values on that line?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error connecting to Unix and running script from Java code

    line d6 session.setPassword(password);

    It is the session value that is null

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Error connecting to Unix and running script from Java code

    Its value is returned by the getSession() method. You need to read the API doc for the method to see why it returns a null value.
    I don't know anything about the packages, classes and methods the code is using.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Running Program in UNIX with package
    By dicdic in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 13th, 2014, 12:06 AM
  2. running script in Postgres via JDBC
    By varu in forum JDBC & Databases
    Replies: 8
    Last Post: November 4th, 2013, 12:21 PM
  3. running script in Postgres via JDBC
    By varu in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 1st, 2013, 01:52 AM
  4. [SOLVED] Making Binary Converter script from scratch, running into math issue.
    By mwebb in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 8th, 2011, 07:47 PM
  5. PRoblem Running Executable script in linux
    By ntu in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: May 2nd, 2011, 06:06 AM