最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

【终极解决】fop错误:Exception in thread "main" java.lang.NoClassDefFoundError:org/apache/xmlgraphics/image/loader/ImageContext 的终极解决办法,即cygpath有bug,转换路径出错,导致部分路径被截断

Docbook crifan 1534浏览 0评论

【问题】

详细的背景参见:

【已彻底解决】docbook中cygwin/linux下的fop运行出错:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlgraphics/image/loader/ImageContext

根本原因后来找到了,是fop中cygpath有bug,导致转换后路径错误,部分路径被截断

此处简述一下背景:

在折腾docbook,用fop生成pdf,结果由于fop中cygpath有bug,会将

/home/CLi/develop/docbook/tools/fop/lib/xmlgraphics-commons-1.4.jar

处理成:

/cygdrive/d/ome/CLi/develop/docbook/tools/fop/lib/xmlgraphics-commons-1.4.jar

很明显此路径是错误的,和其他的正常的路径相比,少了一部分:

/cygdrive/d/  ome/CLi/develop/docbook/tools/fop/lib/xmlgraphics-commons-1.4.jar:
            /home/CLi/develop/docbook/tools/fop/lib/xml-apis-ext-1.3.04.jar:
            /home/CLi/develop/docbook/tools/fop/lib/xml-apis-1.3.04.jar:
            /home/CLi/develop/docbook/tools/fop/lib/xercesImpl-2.7.1.jar:
            /home/CLi/develop/docbook/tools/fop/lib/xalan-2.7.0.jar:
            /home/CLi/develop/docbook/tools/fop/lib/serializer-2.7.0.jar:
            /home/CLi/develop/docbook/tools/fop/lib/commons-logging-1.0.4.jar:
            /home/CLi/develop/docbook/tools/fop/lib/commons-io-1.3.1.jar:
            /home/CLi/develop/docbook/tools/fop/lib/batik-all-1.7.jar:
            /home/CLi/develop/docbook/tools/fop/lib/avalon-framework-4.2.0.jar:
            /home/CLi/develop/docbook/tools/fop/build/fop.jar:
            /home/CLi/develop/docbook/tools/fop/build/fop-sandbox.jar:
            /home/CLi/develop/docbook/tools/fop/build/fop-hyph.jar:
            .:
            /cygdrive/c/Program Files (x86)/Java/jre6/lib/rt.jar

因此,会导致fop报错:

Exception in thread "main" java.lang.NoClassDefFoundError:org/apache/xmlgraphics/image/loader/ImageContext

之前的解决办法是,把xmlgraphics对应的jar添加到windows的环境变量CLASSPATH中去:

CLASSPATH = .;%JAVA_HOME%\lib\rt.jar;D:\tmp\tmp_dev_root\cgwin\home\CLi\develop\docbook\tools\fop\lib\xmlgraphics-commons-1.4.jar;

但是,很明显,此办法只是治标不治本,无法避免类似其他错误。

此处,想要彻底解决此问题。

【解决过程】

1.去看fop的代码,经过一番的调试,发现的确是如:

classpath suddenly not found with fop script and cygwin

所说的,发生错误的相关代码是:

LCP_TEMP=`cygpath --path --unix "$LOCALCLASSPATH"`

其会将:

LOCALCLASSPATH=/home/CLi/develop/docbook/tools/fop/lib/xmlgraphics-commons-1.4.jar;/home/CLi/develop/docbook/tools/fop/lib/xml-apis-ext-1.3.04.jar;/home/CLi/develop/docbook/tools/fop/lib/xml-apis-1.3.04.jar;/home/CLi/develop/docbook/tools/fop/lib/xercesImpl-2.7.1.jar;/home/CLi/develop/docbook/tools/fop/lib/xalan-2.7.0.jar;/home/CLi/develop/docbook/tools/fop/lib/serializer-2.7.0.jar;/home/CLi/develop/docbook/tools/fop/lib/commons-logging-1.0.4.jar;/home/CLi/develop/docbook/tools/fop/lib/commons-io-1.3.1.jar;/home/CLi/develop/docbook/tools/fop/lib/batik-all-1.7.jar;/home/CLi/develop/docbook/tools/fop/lib/avalon-framework-4.2.0.jar;/home/CLi/develop/docbook/tools/fop/build/fop.jar;/home/CLi/develop/docbook/tools/fop/build/fop-sandbox.jar;/home/CLi/develop/docbook/tools/fop/build/fop-hyph.jar;.;C:\Program Files (x86)\Java\jre6\lib\rt.jar;

处理为:

LCP_TEMP=/cygdrive/d/ome/CLi/develop/docbook/tools/fop/lib/xmlgraphics-commons-1.4.jar:/home/CLi/develop/docbook/tools/fop/lib/xml-apis-ext-1.3.04.jar:/home/CLi/develop/docbook/tools/fop/lib/xml-apis-1.3.04.jar:/home/CLi/develop/docbook/tools/fop/lib/xercesImpl-2.7.1.jar:/home/CLi/develop/docbook/tools/fop/lib/xalan-2.7.0.jar:/home/CLi/develop/docbook/tools/fop/lib/serializer-2.7.0.jar:/home/CLi/develop/docbook/tools/fop/lib/commons-logging-1.0.4.jar:/home/CLi/develop/docbook/tools/fop/lib/commons-io-1.3.1.jar:/home/CLi/develop/docbook/tools/fop/lib/batik-all-1.7.jar:/home/CLi/develop/docbook/tools/fop/lib/avalon-framework-4.2.0.jar:/home/CLi/develop/docbook/tools/fop/build/fop.jar:/home/CLi/develop/docbook/tools/fop/build/fop-sandbox.jar:/home/CLi/develop/docbook/tools/fop/build/fop-hyph.jar:.:/cygdrive/c/Program Files (x86)/Java/jre6/lib/rt.jar

其中的:

/cygdrive/d/ome/CLi/develop/docbook/tools/fop/lib/xmlgraphics-commons-1.4.jar

就是此处错误的路径。

2.对于此种错误,尝试着用:

LCP_TEMP=`cygpath --path --mixed "$LOCALCLASSPATH"`

等办法,但是结果还是无法得到所期望的结果。后经过很长时间的调试,终于找到解决办法了。

先贴出添加了调试代码的fop的全部源码:

#! /bin/sh
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements.  See the NOTICE file distributed with
#   this work for additional information regarding copyright ownership.
#   The ASF licenses this file to You under the Apache License, Version 2.0
#   (the "License"); you may not use this file except in compliance with
#   the License.  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

# Shell script to run FOP, adapted from the Jakarta-Ant project.

rpm_mode=true
fop_exec_args=
no_config=false
fop_exec_debug=false
show_help=false
for arg in "$@" ; do
  if [ "$arg" = "--noconfig" ] ; then
    no_config=true
  elif [ "$arg" = "--execdebug" ] ; then
    fop_exec_debug=true
  elif [ my"$arg" = my"--h"  -o my"$arg" = my"--help"  ] ; then
    show_help=true
    fop_exec_args="$fop_exec_args -h"
  else
    if [  my"$arg" = my"-h"  -o  my"$arg" = my"-help" ] ; then
      show_help=true
    fi
    fop_exec_args="$fop_exec_args \"$arg\""
  fi
done

# Source/default fop configuration
if $no_config ; then
  rpm_mode=false
else
  # load system-wide fop configuration
  if [ -f "/etc/fop.conf" ] ; then
    . /etc/fop.conf
  fi

  # load user fop configuration
  if [ -f "$HOME/.fop/fop.conf" ] ; then
    . $HOME/.fop/fop.conf
  fi
  if [ -f "$HOME/.foprc" ] ; then
    . "$HOME/.foprc"
  fi

  # provide default configuration values
  if [ -z "$rpm_mode" ] ; then
    rpm_mode=false
  fi
  if [ -z "$usejikes" ] ; then
    usejikes=$use_jikes_default
  fi
fi

# Setup Java environment in rpm mode
if $rpm_mode ; then
  if [ -f /usr/share/java-utils/java-functions ] ; then
    . /usr/share/java-utils/java-functions
    set_jvm
    set_javacmd
  fi
fi

# OS specific support.  $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
case "`uname`" in
  CYGWIN*) cygwin=true ;;
  Darwin*) darwin=true
           if [ -z "$JAVA_HOME" ] ; then
             JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
           fi
           ;;
esac

if [ -z "$FOP_HOME" -o ! -d "$FOP_HOME" ] ; then
  ## resolve links - $0 may be a link to fop's home
  PRG="$0"
  progname=`basename "$0"`

  # need this for relative symlinks
  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
      PRG="$link"
    else
      PRG=`dirname "$PRG"`"/$link"
    fi
  done

  FOP_HOME=`dirname "$PRG"`

  # make it fully qualified
  FOP_HOME=`cd "$FOP_HOME" && pwd`
fi

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
  [ -n "$FOP_HOME" ] &&
    FOP_HOME=`cygpath --unix "$FOP_HOME"` &&
    echo "after convert to unix FOP_HOME="$FOP_HOME
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME=`cygpath --unix "$JAVA_HOME"` &&
    echo "after convert to unix JAVA_HOME="$JAVA_HOME
fi

if [ "$OS" = "Windows_NT" ] ; then
    pathSepChar=";"
else
    pathSepChar=":"
fi

if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
      echo "JAVACMD="$JAVACMD
    fi
  else
    JAVACMD=`which java 2> /dev/null `
    if [ -z "$JAVACMD" ] ; then
        JAVACMD=java
    fi
  fi
fi

if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly."
  echo "  We cannot execute $JAVACMD"
  exit 1
fi

if [ -n "$CLASSPATH" ] ; then
  echo "CLASSPATH is not null, then calc LOCALCLASSPATH"
  #LOCALCLASSPATH=$CLASSPATH
  if $cygwin; then
    LOCALCLASSPATH=`cygpath --path --unix "$CLASSPATH"`
  else
    LOCALCLASSPATH=$CLASSPATH
  fi
  echo "after got value from CLASSPATH, LOCALCLASSPATH="$LOCALCLASSPATH
fi

# add fop.jar, fop-sandbox and fop-hyph.jar, which reside in $FOP_HOME/build
LOCALCLASSPATH=${FOP_HOME}/build/fop.jar${pathSepChar}${FOP_HOME}/build/fop-sandbox.jar${pathSepChar}${FOP_HOME}/build/fop-hyph.jar${pathSepChar}$LOCALCLASSPATH
echo "after add fop.jar, fop-sandbox and fop-hyph.jar, LOCALCLASSPATH="$LOCALCLASSPATH

# add in the dependency .jar files, which reside in $FOP_HOME/lib
OLD_IFS=$IFS
IFS="
"
DIRLIBS=${FOP_HOME}/lib/*.jar

echo "DIRLIBS="$DIRLIBS

for i in ${DIRLIBS}
do
    # if the directory is empty, then it will return the input string
    # this is stupid, so case for it
    if [ "$i" != "${DIRLIBS}" ] ; then
      if [ -z "$LOCALCLASSPATH" ] ; then
        LOCALCLASSPATH=$i
      else
        LOCALCLASSPATH="$i"${pathSepChar}$LOCALCLASSPATH
        echo "current LOCALCLASSPATH="$LOCALCLASSPATH
      fi
    fi
done
IFS=$OLD_IFS

# add in user-defined hyphenation JARs
if [ -n "$FOP_HYPHENATION_PATH" ] ; then
  echo "before FOP_HYPHENATION_PATH, LOCALCLASSPATH="$LOCALCLASSPATH
  LOCALCLASSPATH=$LOCALCLASSPATH${pathSepChar}$FOP_HYPHENATION_PATH
  echo "after FOP_HYPHENATION_PATH, LOCALCLASSPATH="$LOCALCLASSPATH
fi

# For Cygwin, switch paths to appropriate format before running java
# For PATHs convert to unix format first, then to windows format to ensure
# both formats are supported. Probably this will fail on directories with ;
# in the name in the path. Let's assume that paths containing ; are more
# rare than windows style paths on cygwin.
if $cygwin; then
  if [ "$OS" = "Windows_NT" ] && cygpath -m .>/dev/null 2>/dev/null ; then
    format=mixed
    echo "format is mixed"
  else
    format=windows
    echo "format is mixed"
  fi
  FOP_HOME=`cygpath --$format "$FOP_HOME"`
  echo "FOP_HOME="$FOP_HOME
  #LCP_TEMP=`cygpath --path --unix "$LOCALCLASSPATH"`
  #LCP_TEMP=`cygpath --path --windows "$LOCALCLASSPATH"`
  LCP_TEMP=`cygpath --path --mixed "$LOCALCLASSPATH"`
  LCP_TEMP=`cygpath --path --unix "$LCP_TEMP"`
  echo "----------LCP_TEMP="$LCP_TEMP
  LOCALCLASSPATH=`cygpath --path --$format "$LCP_TEMP"`
  echo "+++++++++ LOCALCLASSPATH="$LOCALCLASSPATH
  if [ -n "$CLASSPATH" ] ; then
    echo "CLASSPATH is not null="$CLASSPATH
    CP_TEMP=`cygpath --path --unix "$CLASSPATH"`
    echo "CP_TEMP="$CP_TEMP
    CLASSPATH=`cygpath --path --$format "$CP_TEMP"`
    echo "CLASSPATH="$CLASSPATH
  fi
  CYGHOME=`cygpath --$format "$HOME"`
fi

# Show script help if requested
if $show_help ; then
  fop_exec_args=""
  echo $0 '[script options] [FOP options]'
  echo 'Script Options:'
  echo '  --help, -h             print this message and FOP help'
  echo '  --noconfig             suppress sourcing of /etc/fop.conf,'
  echo '                         $HOME/.fop/fop.conf, and $HOME/.foprc'
  echo '                         configuration files'
  echo '  --execdebug            print FOP exec line generated by this'
  echo '                         launch script'
fi

# add a second backslash to variables terminated by a backslash under cygwin
if $cygwin; then
  case "$FOP_HOME" in
    *\\ )
    FOP_HOME="$FOP_HOME\\"
    ;;
  esac
  case "$CYGHOME" in
    *\\ )
    CYGHOME="$CYGHOME\\"
    ;;
  esac
  case "$LOCALCLASSPATH" in
    *\\ )
    LOCALCLASSPATH="$LOCALCLASSPATH\\"
    ;;
  esac
  case "$CLASSPATH" in
    *\\ )
    CLASSPATH="$CLASSPATH\\"
    ;;
  esac
fi

# The default commons logger for JDK1.4 is JDK1.4Logger.
# To use a different logger, uncomment the one desired below
# LOGCHOICE=-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog
# LOGCHOICE=-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
# LOGCHOICE=-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

# Logging levels
# Below option is only if you are using SimpleLog instead of the default JDK1.4 Logger.
# To set logging levels for JDK 1.4 Logger, edit the %JAVA_HOME%/JRE/LIB/logging.properties 
# file instead.
# Possible SimpleLog values:  "trace", "debug", "info" (default), "warn", "error", or "fatal".
# LOGLEVEL=-Dorg.apache.commons.logging.simplelog.defaultlog=INFO

# Execute FOP using eval/exec to preserve spaces in paths,
# java options, and FOP args
fop_exec_command="exec \"$JAVACMD\" $LOGCHOICE $LOGLEVEL -classpath \"$LOCALCLASSPATH\" $FOP_OPTS org.apache.fop.cli.Main $fop_exec_args"
if $fop_exec_debug ; then
    echo $fop_exec_command
fi
eval $fop_exec_command

3. 最后找到的解决方法就是:

把原先 Line 189的:

LCP_TEMP=`cygpath --path --unix "$LOCALCLASSPATH"`

改为:

  #LCP_TEMP=`cygpath --path --unix "$LOCALCLASSPATH"`
  # for cygpath has bug, so use follow workaround
  # edit by admin AT crifan DOT com
  LCP_TEMP=`cygpath --path --$format "$LOCALCLASSPATH"`
  LCP_TEMP=`cygpath --path --unix "$LCP_TEMP"`

这样,就可以正常生成所期望的路径了:

after use fixed mode to parse LOCALCLASSPATH, LCP_TEMP=D:/tmp/tmp_dev_root/cgwin/home/CLi/develop/docbook/tools/fop/lib/xmlgraphics-commons-1.4.jar;/home/CLi/develop/docbook/tools/fop/lib/xml-apis-ext-1.3.04.jar;/home/CLi/develop/docbook/tools/fop/lib/xml-apis-1.3.04.jar;/home/CLi/develop/docbook/tools/fop/lib/xercesImpl-2.7.1.jar;/home/CLi/develop/docbook/tools/fop/lib/xalan-2.7.0.jar;/home/CLi/develop/docbook/tools/fop/lib/serializer-2.7.0.jar;/home/CLi/develop/docbook/tools/fop/lib/commons-logging-1.0.4.jar;/home/CLi/develop/docbook/tools/fop/lib/commons-io-1.3.1.jar;/home/CLi/develop/docbook/tools/fop/lib/batik-all-1.7.jar;/home/CLi/develop/docbook/tools/fop/lib/avalon-framework-4.2.0.jar;/home/CLi/develop/docbook/tools/fop/build/fop.jar;/home/CLi/develop/docbook/tools/fop/build/fop-sandbox.jar;/home/CLi/develop/docbook/tools/fop/build/fop-hyph.jar;.;C;D:/Program Files (x86)/Java/jre6/lib/rt.jar;
after reparse LCP_TEMP using unix mode, LCP_TEMP=/home/CLi/develop/docbook/tools/fop/lib/xmlgraphics-commons-1.4.jar:/home/CLi/develop/docbook/tools/fop/lib/xml-apis-ext-1.3.04.jar:/home/CLi/develop/docbook/tools/fop/lib/xml-apis-1.3.04.jar:/home/CLi/develop/docbook/tools/fop/lib/xercesImpl-2.7.1.jar:/home/CLi/develop/docbook/tools/fop/lib/xalan-2.7.0.jar:/home/CLi/develop/docbook/tools/fop/lib/serializer-2.7.0.jar:/home/CLi/develop/docbook/tools/fop/lib/commons-logging-1.0.4.jar:/home/CLi/develop/docbook/tools/fop/lib/commons-io-1.3.1.jar:/home/CLi/develop/docbook/tools/fop/lib/batik-all-1.7.jar:/home/CLi/develop/docbook/tools/fop/lib/avalon-framework-4.2.0.jar:/home/CLi/develop/docbook/tools/fop/build/fop.jar:/home/CLi/develop/docbook/tools/fop/build/fop-sandbox.jar:/home/CLi/develop/docbook/tools/fop/build/fop-hyph.jar:.:C:/cygdrive/d/Program Files (x86)/Java/jre6/lib/rt.jar

此时,fop的最后去exec java,使用上述产生的LOCALCLASSPATH,就可以正常执行了,就可以找到对应的所有的jar了,因为所有的路径都是正确的了。

【总结】

cygpath中的cygpath工具,看来还是不够稳定啊,弄出个这个bug,路径转换有问题,导致N多人使用fop,出现java.lang.NoClassDefFoundError的错误,不是很熟悉这套系统的话,那真的是让人摸不到头脑,因为会出现,所有的路径都设置正确了,结果fop还是出现找不到java库的事情。

再简单说一下解决办法:

对于fop的错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlgraphics/image/loader/ImageContext

表面原因:xmlgraphics的库找不到,即没有找到fop\lib\xmlgraphics-commons-1.4.jar

根本原因:cygpath有bug,路径转换出错,导致xmlgraphics-commons-1.4.jar的路径转换后,出错了,所以才找不到xmlgraphics-commons-1.4.jar。

解决办法:

修改fop文件,将(大概是189行的):

LCP_TEMP=`cygpath --path --unix "$LOCALCLASSPATH"`

改为:

  #LCP_TEMP=`cygpath --path --unix "$LOCALCLASSPATH"`
  # for cygpath has bug, so use follow workaround
  # edit by admin AT crifan DOT com
  LCP_TEMP=`cygpath --path --$format "$LOCALCLASSPATH"`
  LCP_TEMP=`cygpath --path --unix "$LCP_TEMP"`

即可。

然后cygpath就可以正确转换路径了。

就不用再像之前一样,还要单独的把xmlgraphics-commons-1.4.jar的绝对路径,添加到CLASSPATH中了。

转载请注明:在路上 » 【终极解决】fop错误:Exception in thread "main" java.lang.NoClassDefFoundError:org/apache/xmlgraphics/image/loader/ImageContext 的终极解决办法,即cygpath有bug,转换路径出错,导致部分路径被截断

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
54 queries in 0.292 seconds, using 12.35MB memory