Friday, April 17, 2009

deploying with sftp or scp using ant ... Could not load a dependent class com/jcraft/jsch/UserInfo

Have you ever gotten this error?
Could not load a dependent class com/jcraft/jsch/UserInfo
You are not alone.

You will find a page here that is supposed to help you, but well, let's just say that technical people have a problem communicating sometimes. This problem can also occur if you are trying to run an ant task from within Eclipse that tries to scp or sftp some files.

So below, I will show you how I solved this problem by downloading the Java Secure Channel jar from here, and putting that jar in the $ANT_HOME/lib directory. For the solution to this same problem from within Eclipse, see the section below entitled Solving the problem for Eclipse.

The error and the solution:

The following box shows the build.xml file used, then the ant command line used to reproduce the error, which is immediately followed by the commands needed to solve the issue, and then the successful sftp of files to a machine called, "remoteBox".
$ cat build.xml
<project name="Life" basedir=".">
<target name="deploy">
<scp todir="${user}:${pass}@remoteBox.com:." sftp="true" >
<fileset dir=".">
<include name="**/foo.*"/>
</fileset>
</scp>
</target>
</project>

$ ant -Duser=geek -Dpass=g33k deploy
Buildfile: build.xml

deploy:

BUILD FAILED
build.xml:4: Problem: failed to create task or type scp
Cause: Could not load a dependent class com/jcraft/jsch/UserInfo
It is not enough to have Ant's optional JARs
you need the JAR files that the optional tasks depend upon.
Ant's optional task dependencies are listed in the manual.
Action: Determine what extra JAR files are needed, and place them in one of:
-$ANT_HOME\lib
-$HOME\.ant\lib
-a directory added on the command line with the -lib argument

Do not panic, this is a common problem.
The commonest cause is a missing JAR.

This is not a bug; it is a configuration problem


Total time: 0 seconds

$ cp jsch-0.1.41.jar $ANT_HOME/lib/.

$ ant -Duser=geek -Dpass=g33k deploy
Buildfile: build.xml

deploy:
[scp] Connecting to remoteBox.com:22
[scp] done.

BUILD SUCCESSFUL
Total time: 2 seconds

So, the procedure above shows:
1. the ant build target I used with the scp task
2. the failure that will occur without the use of the Java Secure Channel jar
3. copying the jar to your $ANT_HOME/lib directory
4. the successful transfer of files using sftp

This has been tested with ant 1.6.5 and 1.7.1, although you will get the following error if you use sftp="true" with ant 1.6.5:
$ ant -Duser=geek -Dpass=g33k deploy
Buildfile: build.xml

deploy:

BUILD FAILED
build.xml:4: The type doesn't support the "sftp" attribute.

Total time: 0 seconds

If you simply remove the sftp="true" attribute from the scp task, then the file tranfer works fine under ant 1.6.5 using the build target as shown above.

Solving the problem for Eclipse

Depending on your version of Eclipse, the exact menu option may vary, but it goes something like this in the Galileo version of Eclipse: Go to Window > Preferences > Ant > Runtime > Classpath (tab), select “Ant Home Entries (default)” and click “Add External JARs…”, then add the Java Secure Channel jar from above Then click Apply and Ok. Now Eclipse should be able to run ant tasks which scp or sftp files as noted above.

Hope that helps.

1 comment:

Steve said...

Hey, just wanted to say thanks for the clarification on this process. I tried using ant-jsch included with Ganymede and muddled about with libjsch-java and didn't get anywhere until doing as you said by downloading jsch directly from the author's site (a new version is out, by the way - your link is a tiny bit outdated). I've bookmarked this page in case I run into this problem in the future (probably will the next time I do a fresh Eclipse install).