Tuesday 3 January 2012

Automate File/script tasks on Remote SFTP Server using JSCH

There are requirements when we need to upload/copy files to SFTP server and perform several tasks(e.g. run some commands or scripts, comping files..etc) after uploading the file.

As this is Secure FTP server we cannot use simple multipart request to upload file in the server.

Manual approach to copy files and run scripts/commands on the file in remote SFTP server is time consuming as we need to write all the script commands manually.

To overcome manual efforts JAVa provides API called JSCH (Java Secure Channel).

This API is used for the copying (uploading) any file and executing tasks such as commands/scripts to Remote Server which is enabled with a Secure FTP.

import com.jcraft.jsch.ChannelSftp;

To get the Sesssion for remote SFTP server,

jsch.getSession(username, host, 22);
/* username= sever User name which is used to connect  the server */
/* host =  IP_Address or Servername*/
 session.setPassword(pass);
 /*pass = password to connect  the remote server*/

Once we get the Session for the Remote Server, the next step is to
open the SFTP channel by,

session.connect();
channel = session.openChannel("sftp");
channel.connect();
c = (ChannelSftp) channel;
once we get the SFTP channel we can perform tasks which were perfromed manually
c.rm("fileName");
c.put("source","destination");

To execute commands on the SFTP server,the exec channel needs to be opened by
Channel channel=session.openChannel("exec");
and then we can run single/multiple commnands by below statement.
((ChannelExec) channel).setCommand(cmd1+"\n"+cmd2+"\n"+cmd3);
where cmd1, cmd2 and cmd3 can be any commands like
String cmd1="cd /Test/Scripts/";
String cmd2 ="chmod 777 test.sh";
String cmd3 = "./test.sh "+Command Argument;

once the files are copied/uploaded successfully, at the end,sftp
session and channel needs to be disconnected.

Friday 14 October 2011

Solution to org.xml.sax.SAXParseException: Document root element is missing

While parsing to XML files with SAX Parser in JAVA, the common exception occurs as Document Root Element is missing.
If the XML is well formed then the exception can occur because it is created in UNIX environment and you are trying to parse in windows environment.


There can be three solutions for this exception if it is well-formed
1) Manual
2) Using setEncoding Method
3) Using Script in your java code.

Manual Solution:
First solution is to open xml in Notepad and hit the enter at the end of file.so it will add one blank line after end of root element.
But if you have more than one file to parse above manual option is not a good option.

setEncoding Method:
This method is used in before calling parser.parse method.
we need to call setEncoding("UTF-8") method  beofre parsing XML file.
 Using VBScript 
The better solution is to call below vbscript from your java code so it will overcome all manual task for adding new line.
--------------------------------------------------------------------------
dim file_name
Set objFS = createObject("Scripting.FileSystemObject")
file_Folder = WScript.Arguments.Item(0)   
Set xmlDom = CreateObject("Microsoft.XMLDOM")
      xmlDom.async = False
      xmlDom.Load(Your XML file with full path)
      xmlDom.Load(Yout XML file with full path)
---------------------------------------------------------------------------
The above script can be call from your java code using Runtime.getRuntime.exec() method.


If you have more files in the sub folders you can use recursive function to parser all the XMLs from the parent folder.


This way you can solve the Root Element missing exception. But i believe 2nd option is the best option for solving this exception.