java-class-enumerator icon indicating copy to clipboard operation
java-class-enumerator copied to clipboard

FileNotFoundException if path or file name contains a space while iterating over a jar file

Open christianbalzer opened this issue 10 years ago • 1 comments

I get a FileNotFoundException when going through a jar file if the jar file or its path contain a space:

C:\Users\foobar\test>java -jar "Class Enumeration.jar"
ClassDiscovery: Package: com.example.test.dynamic becomes Path:com/example/test/dynamic
ClassDiscovery: Resource = jar:file:/C:/Users/foobar/test/Class%20Enumeration.jar!/com/example/test/dynamic
ClassDiscovery: FullPath = jar:file:/C:/Users/foobar/test/Class%20Enumeration.jar!/com/example/test/dynamic
ClassDiscovery: Directory = null
Exception in thread "main" java.lang.RuntimeException: com.example.test.dynamic (null) does not appear to be a valid package
        at com.example.test.dynamic.ClassEnumerator.getClassesForPackage(ClassEnumerator.java:80)
        at com.example.test.dynamic.ClassEnumerator.main(ClassEnumerator.java:87)
Caused by: java.io.FileNotFoundException: C:\Users\foobar\test\Class%20Enumeration.jar (The system cannot find the file specified)
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(Unknown Source)
        at java.util.zip.ZipFile.<init>(Unknown Source)
        at java.util.jar.JarFile.<init>(Unknown Source)
        at java.util.jar.JarFile.<init>(Unknown Source)
        at com.example.test.dynamic.ClassEnumerator.getClassesForPackage(ClassEnumerator.java:62)
        ... 1 more

The ClassEnumerator class contains the following main method as well as the methods posted on StackOverflow:

    public static void main(String... args0) {
        getClassesForPackage(Package.getPackage("com.example.test.dynamic"));
    }

I'm running on Win 7, 64bit. Here is my Java version:

java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) Client VM (build 25.20-b23, mixed mode, sharing)

I have the following directory setup

src
+---main
|   +---java
|   |   +---com
|   |   |   \---example
|   |   |       \---test
|   |   |           \---dynamic
|   |   |               |   ClassEnumerator.java
|   |   |               |
|   |   |               \---classes
|   |   |                   |   MainDirTestClass.java
|   |   |                   |
|   |   |                   \---subpackage
|   |   |                           SubPackageTestClass.java
|   |   \---META-INF
|   |           MANIFEST.MF
|   \---resources
\---test
    \---java

christianbalzer avatar Jan 08 '15 14:01 christianbalzer

Maybe you can test this case https://stackoverflow.com/questions/11565694/java-io-filenotfoundexception-on-an-existing-file

saife avatar Feb 01 '18 01:02 saife