ANT - Could not load a dependent class com/jcraft/

2020-08-17 02:51发布

问题:

I have a problem with my Ant script.

I need to copy a file to Linux server

<copy file="../Ant/lib/jsch-0.1.50.jar" tofile="${ant.home}/lib/jsch-0.1.50.jar" />

<scp todir="${server.user}:${server.password}@${server.dev}:${server.dev.dir.config}" trust="true" verbose="true">
    <fileset dir="${src.home}/Config/">
        <include name="**/*" />
    </fileset>
</scp>

File is copied correctly, but I receive this error:

BUILD FAILED
C:\dev.xml:179: Problem: failed to create task or type scp
Cause: Could not load a dependent class com/jcraft/jsch/Logger
       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:
    -C:\Progs\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib
    -C:\Users\Administrator\.ant\lib
    -a directory added on the command line with the -lib argument

How can I dynamically add this JAR in the Ant classloader?

回答1:

The Exception itself is quite clear and points the reason of the problem:

Action: Determine what extra JAR files are needed, and place them in one of:
    -C:\Progs\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib
    -C:\Users\Administrator\.ant\lib
    -a directory added on the command line with the -lib argument

Please check from Library Dependencies

jsch.jar 0.1.50 or later: sshexec and scp tasks (http://www.jcraft.com/jsch/index.html)

If you copy this lib in the ant lib it will solve the issue for the standalone Ant.

And for the case of Eclipse: Go into the Eclipse Preferences, in the Ant/Runtime entry, in the 'Classpath' tab. Use the button 'Add External Jars' to add your jsch.jar to Ant's classpath.



回答2:

I have found the solution here



回答3:

If you use Eclipse as IDE and you run ant from Eclipse is not enough to copy the needed jar file jsch-0.1.51.jar (or whatever version). It is alse needed to change the classpath ant uses when running.

If you use Windows -> Window > Preferences > Ant > Runtime If you use Mac -> Eclipse > Preferences > Ant > Runtime

in the Classpath tab in "Ant home entries..." you must add the new jar.

After that it works.



回答4:

Add all dependencies of JSCH to lib directory of ANT using similar <copy tag.

To find dependencies of JSCH check its documentation.



回答5:

After you have copied jsch-0.1.51.jar to /usr/share/ant/lib/ (or whatever directory you have), make the JAR file readable for all users, not just root. Don't be like me and spend half an hour trying to figure out why it doesn't work.