开发者

Ant, Tomcat Build Error: java.lang.NoClassDefFoundError: org/apache/tomcat/util/buf/B2CConverter

开发者 https://www.devze.com 2023-04-01 21:43 出处:网络
Tomcat Version: 7.0.20 I am attempting to work my way through the following Spring MVC tutorial: http://static.springsource.org/docs/Spring-MVC-step-by-step/part1.html

Tomcat Version: 7.0.20

I am attempting to work my way through the following Spring MVC tutorial: http://static.springsource.org/docs/Spring-MVC-step-by-step/part1.html

In this tutorial, an ant build script is setup to deploy to tomcat using the manager. However, I run into some problems when I try to run any of the tomcat tasks.

First off, in the tutorial, they still use org.apache.catalina.ant.InstallTask which is deprecated, so I changed to org.apache.catalina.ant.DeployTask.

Now the problem is that when trying to run the Tomcat tasks I get:

java.lang.NoClassDefFoundError: org/apache/tomcat/util/buf/B2CConverter
    at org.apache.catalina.util.Base64.encode(Base64.java:177)
    at org.apache.catalina.ant开发者_运维百科.AbstractCatalinaTask.execute(AbstractCatalinaTask.java:204)
    at org.apache.catalina.ant.AbstractCatalinaTask.execute(AbstractCatalinaTask.java:150)
    at org.apache.catalina.ant.ReloadTask.execute(ReloadTask.java:45)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:390)
    at org.apache.tools.ant.Target.performTasks(Target.java:411)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
    at org.apache.tools.ant.Main.runBuild(Main.java:809)
    at org.apache.tools.ant.Main.startAnt(Main.java:217)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
 Caused by: java.lang.ClassNotFoundException: org.apache.tomcat.util.buf.B2CConverter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 21 more

So I Googled around a bit and found some information stating that tomcat-utils.jar should be in the class path so I added that into the fileset with the catalina-ant.jar but apparently the org.apache.tomcat.util.buf.B2CConverter class is not in there.

So next I started probing the jars with jar -tf to find out if any contained the class. I found out that tomcat-coyote.jar had the class. Even with including this in the fileset, the problem is not resolved.

Does anyone have any ideas?


I got this working by changing the classpath to

<path id="catalina-ant-classpath">
    <!-- We need the Catalina jars for Tomcat -->
    <!--  * for other app servers - check the docs --> 
    <fileset dir="${appserver.lib}">
        <include name="catalina-ant.jar"/>
        <include name="tomcat-coyote.jar"/>
        <include name="tomcat-util.jar"/>
    </fileset>
    <fileset dir="${appserver.home}/bin">
                <include name="tomcat-juli.jar"/>
    </fileset>
</path>


Like Jason, I tried Aidan's answer and it did not work. With TomCat 7 the manager interface has changed a little. Instead of using /manager/list in the URL you have to use /manager/text/list. So I changed the list task in build.xml to the following:

<target name="list" description="List Tomcat applications">
    <list url="${tomcat.manager.url}/text"
             username="${tomcat.manager.username}"
             password="${tomcat.manager.password}"/>
</target>

I also had to add the "manager-script" role to my admin user so that it would have the proper privileges for this operation:

  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <user username="admin" password="admin" roles="manager-gui,manager-script"/>


I found the same error when following the same tutorial. I had to make 3 changes so that the targets run correctly.

First, change the build.xml "list" target to add the /text above mentioned:

<target name="list" description="List Tomcat applications">
    <list url="${tomcat.manager.url}/text"
          username="${tomcat.manager.username}"
          password="${tomcat.manager.password}"/>
</target>

Second, change the catalina ant classpath:

<path id="catalina-ant-classpath">
    <fileset dir="${appserver.home}/lib">
        <include name="catalina-ant.jar"/>
        <include name="tomcat-coyote.jar"/>
        <include name="tomcat-util.jar"/>
    </fileset>
    <fileset dir="${appserver.home}/bin">
        <include name="tomcat-juli.jar"/>
    </fileset>
</path>

And finally, add this to the tomcat-users.xml file:

<role rolename="manager-script"/>
  <user username="tomcat" password="s3cret" roles="manager-gui,tomcat,manager-script"/>

And then it worked for me =)


This seemed to work for me (Tomcat 7.0.21, OS X):

<path id="catalina-ant-classpath">
  <fileset dir="${tomcat_dir}/lib">
    <include name="catalina-ant.jar"/>
    <include name="tomcat-coyote.jar"/>
    <include name="tomcat-util.jar"/>
  </fileset>
  <fileset dir="${tomcat_dir}/bin">
    <include name="tomcat-juli.jar"/>
  </fileset>
</path>
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" classpathref="catalina-ant-classpath"/>
<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" classpathref="catalina-ant-classpath"/>
<taskdef name="list" classname="org.apache.catalina.ant.ListTask" classpathref="catalina-ant-classpath"/>


In order to get all the ant commands to work the manager url can be changed in the build.properties file instead of changing the url when defining the "list" target in the build.xml file.

tomcat.manager.url=http://localhost:8080/manager/text


None of the other answer work for me. Instead I copied TOMCAT_HOME/lib/catalina-ant.jar, TOMCAT_HOME/lib/tomcat-coyote.jar, TOMCAT_HOME/lib/tomcat-util.jar and TOMCAT_HOME/bin/tomcat-juli.jar to the ANT_HOME/lib directory.

It seems like the classpathref doesn't include these files properly.


what worked for me was adding the four jar files to the build path

Run -> run configuration -> choose you program, select Classpath tab on the right, click User Entries, then add external 4 jars from the tomcat directory

After this my build.xml runs like before


I'm not sure why is this happening, but here's a workaround: http://www.coderanch.com/t/553582/Tomcat/java-lang-NoClassDefFoundError-org-apache

I'm looking for the solution, once I get it I will try not to forget to post it in here.

I use opensuse.


I am on Mac OS X. This worked for me:

<!-- ============================================================== -->
<!-- Tomcat tasks - remove these if you don't have Tomcat installed -->
<!-- ============================================================== -->

    <path id="catalina-ant-classpath">
        <!-- We need the Catalina jars for Tomcat -->
        <!--  * for other app servers - check the docs -->
        <fileset dir="${appserver.lib}">
            <include name="catalina-ant.jar"/>
        <include name="tomcat-coyote.jar"/>
        <include name="tomcat-util.jar"/>
      </fileset>
      <fileset dir="${appserver.home}/bin">
        <include name="tomcat-juli.jar"/>
      </fileset>        
    </path>


I new to Spring or even first use to tomcat, i'm just trying add classpath to subsection tomcat task in build.xml (in every tomcat task) and i dont why, but its works.

<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask">
    <classpath>
        <path location="${appserver.home}/lib/catalina-ant.jar"/>
        <path location="${appserver.home}/lib/tomcat-coyote.jar"/>
        <path location="${appserver.home}/lib/tomcat-util.jar"/>
        <path location="${appserver.home}/bin/tomcat-juli.jar"/>
    </classpath>
</taskdef>


After doing above changes and running "ant list", I got error as, java.io.FileNotFoundException: ..../manager/text/list". It was resolved by starting tomcat from its installation directory and not using eclipse to start it. After starting tomcat, first try "localhost:8080" in the browser. This should display tomcats page. Then try running "ant list". It works and above issue is resolved.


I would suggest a minor modification to the previous answers.

Configuring a user to have both manager-gui and manager-script roles is strongly discouraged in the Tomcat 7 documentation 1. It can leave your site open to CSRF attacks.

Defining separate users with different passwords for each role would be safer:

<role rolename="manager-"/>
<role rolename="manager-script"/>
<user username="admin-gui" password="s3cret1" roles="manager-gui"/>
<user username="admin-script" password="s3cret2" roles="manager-script"/>

In general, you only need to memorize the user name and password for the GUI user, so it's no burden to have a safer configuration.


Three Things-

  • If using Tomcat 7.x Change fileset for catalina-ant-classpath

    <path id="catalina-ant-classpath">
        <!-- We need the Catalina jars for Tomcat -->
        <!--  * for other app servers - check the docs -->
        <fileset dir="${appserver.lib}">
            <include name="catalina-ant.jar"/>
        <include name="tomcat-coyote.jar"/>
        <include name="tomcat-util.jar"/>
      </fileset>
      <fileset dir="${appserver.home}/bin">
        <include name="tomcat-juli.jar"/>
      </fileset>        
    

  • change the user in tomcat-users.xml to give user permission as manager-script

  • Change tomcat.manager.url property to http://localhost:8080/manager/text


Three Things-

If using Tomcat 7.x Change fileset for catalina-ant-classpath

    <path id="catalina-ant-classpath">
    <fileset dir="${appserver.lib}">
        <include name="catalina-ant.jar"/>
    <include name="tomcat-coyote.jar"/>
    <include name="tomcat-util.jar"/>
  </fileset>
  <fileset dir="${appserver.home}/bin">
    <include name="tomcat-juli.jar"/>
  </fileset> 
</path> 

change the user in tomcat-users.xml to give user permission as manager-script

Change tomcat.manager.url property to http://localhost:8080/manager/text


You just need to add catalina-ant.jar tomcat-coyote.jar and tomcat-util.jar from tomcat_home/lib/ directory to ant_home/lib/ directory

0

精彩评论

暂无评论...
验证码 换一张
取 消