为什么Tomcat会为现有的JAR文件抛出FileNotFoundExceptions?

时间:2015-02-06 08:35:36

标签: java maven tomcat jar native

我想知道什么可能导致Tomcat或本机Java ZipFile.open 声称文件实际上不存在的方法?这个 在过去的一个月里,我的一些工作一直是阻碍问题。它的 在尝试运行tomcat7-maven-plugin时发生。它工作正常 在大多数机器上,包括我的(OSX),但在我们的构建服务器上失败 (LINUX)和我的一个同事的盒子(OSX,和我的相同型号的笔记本电脑)。 这是Maven构建中出现的错误:

[INFO] --- tomcat7-maven-plugin:2.2:run (start-tomcat) @ PROJECT ---
[INFO] Running war on http://localhost:8080/contentmain
[INFO] Using existing Tomcat server configuration at
/WORKSPACE/PROJECT/tomcat7
Feb 05, 2015 11:17:53 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal
performance in production environments was not found on the
java.library.path:
/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Feb 05, 2015 11:17:54 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Feb 05, 2015 11:17:54 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Feb 05, 2015 11:17:54 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8443"]
Feb 05, 2015 11:17:54 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 651 ms
Feb 05, 2015 11:17:54 PM org.apache.catalina.core.StandardService
startInternal
INFO: Starting service Catalina
Feb 05, 2015 11:17:54 PM org.apache.catalina.core.StandardEngine
startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.53
Feb 05, 2015 11:17:54 PM org.apache.tomcat.util.scan.StandardJarScanner
scan
WARNING: Failed to scan JAR
[file:/WORKSPACE/tomcat7/webapps/../../target/PROJECT/WEB-INF/lib/openws-1.
5.1.jar] from WEB-INF/lib
java.io.FileNotFoundException:
/WORKSPACE/PROJECT/tomcat7/webapps/../../target/PROJECT/WEB-INF/lib/openws-
1.5.1.jar (No such file or directory)
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:215)
    at java.util.zip.ZipFile.<init>(ZipFile.java:145)
    at java.util.jar.JarFile.<init>(JarFile.java:154)
    at java.util.jar.JarFile.<init>(JarFile.java:91)
    at sun.net.www.protocol.jar.URLJarFile.<init>(URLJarFile.java:93)
    at sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:69)
    at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:99)
    at
sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122
)
    at
sun.net.www.protocol.jar.JarURLConnection.getJarFile(JarURLConnection.java:
89)
    at org.apache.tomcat.util.scan.FileUrlJar.<init>(FileUrlJar.java:41)
    at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:34)
    at
org.apache.catalina.startup.ContextConfig$FragmentJarScannerCallback.scan(C
ontextConfig.java:2612)
    at
org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.j
ava:259)
    at
org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java
:178)
    at
org.apache.catalina.startup.ContextConfig.processJarsForWebFragments(Contex
tConfig.java:1868)
    at
org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1256
)
    at
org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java
:873)
    at
org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java
:371)
    at
org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSuppo
rt.java:117)
    at
org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.jav
a:90)
    at
org.apache.catalina.core.StandardContext.startInternal(StandardContext.java
:5355)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at
org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1
559)
    at
org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1
549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1
145)
    at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:
615)
    at java.lang.Thread.run(Thread.java:745)

对于已拉入的项目中的每个JAR文件,都会重复此错误 来自Maven依赖。这些有数百种。

请注意,我在其中一个ZipFile.open电话上放了一个断点 失败的机器并在一个单独的终端中执行以下操作:

cd /WORKSPACE/PROJECT/tomcat7/webapps/../../target/PROJECT/WEB-INF/lib/
ls -la

我可以确认所有“丢失的”JAR文件都立即存在 在调用本机open方法之前,抛出异常 声称文件不在那里。这让我怀疑了 JAR文件可能已损坏,因此我复制了其中一个失败的JAR文件 我的同事的机器,并用我当地的副本进行了差异化 在成功构建和执行之后。他们是相同的,所以它 似乎“腐败的JAR”理论已经出局。

我也尝试了以下(没有成功):

  • 缩短使用的路径(我读到某处可能有256个 字符限制)。
  • 将JAR文件移动到其他目录并更改Tomcat 配置指向新位置(它们在资源和 我把它们移到了项目base.dir)。
  • 将各种JVM堆大小从Xmx256设置为Xmx4096(我在下面阅读了几篇 发布内存问题的帖子可能导致Tomcat声称文件丢失了。)
  • 删除我们通常用于webapp的permgen设置。
  • 将“jarScanAllDirectories”选项设置为false或true tomcat7-maven-plugin。
  • 下载tomcat-maven-plugin的源代码(包括 来自Apache SVN的tomcat7-maven-plugin),用遥控器连接它们 调试程序到Maven并逐步执行(一切似乎 在本机ZipFile.open调用之前一直相同。
  • 在构建中使用Maven的各种Jenkins作业设置 服务器(可能无关紧要,因为这也在同事的机器上失败了 没有詹金斯参与)。
  • 比较我们的webapp使用的所有环境变量 我的同事使用它们(它们是相同的)。
  • 比较JDK版本(我们的整个组织在1.7.0_45标准化)。
  • 比较Tomcat版本(我们有明确的pom.xml文件依赖项 版本7.0.53上的tomcat7-maven-plugin。
  • 等待并希望问题解决。
  • 要求我的所有同事看一看(这导致尝试了一些 以上,但问题仍未解决)。

我在这里结束了我的智慧。我已经过了几天午夜了 只是想让这件事情发挥作用。剩下要看什么了?

更新(2015年2月10日):有人建议这是因为Jenkins机器上运行此文件的文件权限丢失。我无法访问发生这种情况的服务器,但我确实在那里运行了一个脚本:

echo Displaying JAR files with current permissions...
ls -la ./target/MyProject/WEB-INF/lib/

echo Adding read, write, and execute permissions to JAR files...
chmod -R 777 ./target/MyProject/WEB-INF/lib/

echo Displaying JAR files with updated permissions...
ls -la ./target/MyProject/WEB-INF/lib/

这会产生如下输出:

[INFO] --- exec-maven-plugin:1.3.2:exec (update_jar_file_permissions) @
MyProject ---
Displaying JAR files with current permissions...
total 124556
drwxr-xr-x 2 jenkins users    20480 Feb 10 17:26 .
drwxr-xr-x 6 jenkins users     4096 Feb 10 17:26 ..
-rw-r--r-- 1 jenkins users    62983 Jan 22 00:11 activation-1.1.jar
-rw-r--r-- 1 jenkins users   351656 Jan 22 00:25 amqp-client-3.1.3.jar
-rw-r--r-- 1 jenkins users    74080 Jan 22 00:25 annotations-2.0.0.jar
-rw-r--r-- 1 jenkins users   445288 Jan 22 00:25 antlr-2.7.7.jar
-rw-r--r-- 1 jenkins users   895124 Jan 22 00:25 antlr-3.2.jar

Adding read, write, and execute permissions to JAR files...
Displaying JAR files with updated permissions...
total 124556
drwxrwxrwx 2 jenkins users    20480 Feb 10 17:26 .
drwxr-xr-x 6 jenkins users     4096 Feb 10 17:26 ..
-rwxrwxrwx 1 jenkins users    62983 Jan 22 00:11 activation-1.1.jar
-rwxrwxrwx 1 jenkins users   351656 Jan 22 00:25 amqp-client-3.1.3.jar
-rwxrwxrwx 1 jenkins users    74080 Jan 22 00:25 annotations-2.0.0.jar
-rwxrwxrwx 1 jenkins users   445288 Jan 22 00:25 antlr-2.7.7.jar
-rwxrwxrwx 1 jenkins users   895124 Jan 22 00:25 antlr-3.2.jar

如您所见, 缺少权限。但是,事实并非如此 解决了这个问题:

Feb 10, 2015 5:27:54 PM org.apache.tomcat.util.scan.StandardJarScanner scan
WARNING: Failed to scan JAR [file:/opt/jenkins/workspace/MY_JENKINS_JOB/tomcat7/webapps/../../target/MY_PROJECT/WEB-INF/lib/activation-1.1.jar] from WEB-INF/lib
java.io.FileNotFoundException: /opt/jenkins/workspace/MY_JENKINS_JOB/tomcat7/webapps/../../target/MY_PROJECT/WEB-INF/lib/activation-1.1.jar (No such file or directory)
at java.util.zip.ZipFile.open(Native Method)

脚本在tomcat7-maven-plugin之前运行。权限应该已经到位,JAR文件应该是可提取的。我仍然不明白为什么这不起作用。

1 个答案:

答案 0 :(得分:2)

事实证明这根本不是权限问题。出于某种原因,将JAR文件复制出目录并返回到目录中导致它们被拾取(我在某处的建议中读到了这个)。读取cp的'man'条目我看到它不保留“访问控制列表(ACL)和扩展属性(EA),包括资源分支”,除非设置了-p标志(默认情况下,这在使用mv时打开)。我的猜测是删除这个“访问控制”信息以某种方式使tomcat7-maven-plugin可以访问这些文件。我真的不知道问题的根本原因似乎有点粗略,但我很高兴它现在已经修复了。

如果有人可以明确地解释为什么会有效,那么我会接受这个而不是这个答案。