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

Ale*_*sen 6 java tomcat native jar maven

我想知道什么可能导致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)
Run Code Online (Sandbox Code Playgroud)

对于从Maven依赖项中提取的项目中的每个JAR文件,都会重复此错误.这些有数百种.

请注意,我ZipFile.open在其中一台发生故障的计算机上调用了断点,并在一个单独的终端中执行了以下操作:

cd /WORKSPACE/PROJECT/tomcat7/webapps/../../target/PROJECT/WEB-INF/lib/
ls -la
Run Code Online (Sandbox Code Playgroud)

我可以确认在open调用本机方法之前,所有"丢失的"JAR文件都存在,并且抛出了声称文件不存在的异常.这让我怀疑JAR文件可能已损坏,因此我从同事的机器上复制了一个失败的JAR文件,并使用我成功构建和执行后的本地副本执行了差异.它们是相同的,所以似乎"腐败的JAR"理论已经出来了.

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

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

我在这里结束了我的智慧.我已经过了几天,只是试图让这件事情起作用.剩下要看什么了?

更新(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/
Run Code Online (Sandbox Code Playgroud)

这会产生如下输出:

[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
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,没有权限.但是,这还没有解决问题:

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)
Run Code Online (Sandbox Code Playgroud)

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

Ale*_*sen 4

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

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