2008-12-31 20 views
45

Estoy intentando acceder a un archivo XML dentro de un archivo jar, desde un contenedor separado que se ejecuta como una aplicación de escritorio. Puedo obtener la URL del archivo que necesito, pero cuando paso eso a un FileReader (como una cadena) obtengo una excepción FileNotFoundException que dice "El nombre de archivo, el nombre del directorio o la sintaxis de la etiqueta de volumen es incorrecta".¿Cómo leo un archivo de recursos de un archivo jar de Java?

Como punto de referencia, no tengo problemas para leer recursos de imágenes del mismo contenedor, pasando la URL a un constructor ImageIcon. Esto parece indicar que el método que estoy usando para obtener la URL es correcto.

URL url = getClass().getResource("/xxx/xxx/xxx/services.xml"); 
ServicesLoader jsl = new ServicesLoader(url.toString()); 

Dentro de la clase ServicesLoader tengo

XMLReader xr = XMLReaderFactory.createXMLReader(); 
xr.setContentHandler(this); 
xr.setErrorHandler(this); 
xr.parse(new InputSource(new FileReader(filename))); 

¿Qué hay de malo en usar esta técnica para leer el archivo XML?

Respuesta

4

El problema era que iba demasiado lejos al llamar al método parse de XMLReader. El método de análisis acepta un InputSource, por lo que no había ninguna razón para usar un FileReader. Cambiar la última línea del código de arriba a

xr.parse(new InputSource(filename)); 

funciona bien.

5

No dice si se trata de una aplicación de escritorio o web. Usaría el método getResourceAsStream() de un ClassLoader apropiado si es un escritorio o el contexto si es una aplicación web.

+0

Es una aplicación de escritorio. Voy a editar la pregunta. –

0

Fuera de su técnica, ¿por qué no utilizar el estándar Java JarFile class para obtener las referencias que desea? A partir de ahí, la mayoría de tus problemas desaparecerán.

4

Parece como si está utilizando el resultado URL.toString como el argumento de la FileReader constructor. URL.toString está un poco roto, y en su lugar generalmente debe usar url.toURI().toString(). En cualquier caso, la cadena no es una ruta de archivo.

En su lugar, deberán:

  • Pasar el URL a ServicesLoader y deje que se llama openStream o similar.
  • Usa Class.getResourceAsStream y acaba de pasar la corriente, posiblemente dentro de un InputSource. (Recuerde comprobar nulos ya que la API es un poco desordenada.)
0

Si utiliza los recursos extensamente, puede considerar usar Commons VFS.

Se pronuncia a favor: * Archivos locales * FTP, SFTP * HTTP y HTTPS * Archivos temporales "FS normales respaldo) * Zip, tarro y alquitrán (sin comprimir, TGZ o tbz2) * gzip y bzip2 * recursos * ram - "RAMDRIVE" * mimo

también hay JBoss VFS -.. pero no es mucho más documentados

0

tengo 2 archivos CSV que utilizo para leer los datos el programa java se exporta como ar archivo jar no compatible Cuando lo exporte, descubrirá que no exporta sus recursos con él.

Agregué una carpeta bajo proyecto llamada datos en eclipse. En esa carpeta, almacené mis archivos csv.

Cuando necesito para hacer referencia a aquellos archivos que hacerlo de esta manera ...

private static final String ZIP_FILE_LOCATION_PRIMARY = "free-zipcode-database-Primary.csv"; 
private static final String ZIP_FILE_LOCATION = "free-zipcode-database.csv"; 

private static String getFileLocation(){ 
    String loc = new File("").getAbsolutePath() + File.separatorChar + 
     "data" + File.separatorChar; 
    if (usePrimaryZipCodesOnly()){    
     loc = loc.concat(ZIP_FILE_LOCATION_PRIMARY); 
    } else { 
     loc = loc.concat(ZIP_FILE_LOCATION); 
    } 
    return loc; 
} 

A continuación, cuando se pone el frasco en un lugar para que pueda ser corrió a través de comandos, asegúrese de que agrega los datos carpeta con los recursos en la misma ubicación que el archivo jar.

2

Me gustaría señalar que uno de los problemas es qué pasa si los mismos recursos están en varios archivos jar. Digamos que desea leer /org/node/foo.txt, pero no desde un archivo, sino desde cada archivo jar.

Me he encontrado con este mismo problema varias veces antes. Esperaba en JDK 7 que alguien escribiera un sistema de archivos classpath, pero todavía no.

Spring tiene la clase Resource que le permite cargar recursos classpath bastante bien.

Escribí un pequeño prototipo para resolver este problema de leer recursos de varios archivos jar. El prototipo no maneja todos los casos extremos, pero maneja la búsqueda de recursos en los directorios que están en los archivos jar.

He usado Stack Overflow durante bastante tiempo. Esta es la segunda respuesta que recuerdo al responder una pregunta, así que perdóneme si llevo demasiado tiempo (es mi naturaleza).

Este es un lector de recursos prototipo. El prototipo carece de una sólida comprobación de errores.

Tengo dos archivos de jar de prototipo que tengo configurados.

<pre> 
     <dependency> 
       <groupId>invoke</groupId> 
       <artifactId>invoke</artifactId> 
       <version>1.0-SNAPSHOT</version> 
      </dependency> 

      <dependency> 
       <groupId>node</groupId> 
       <artifactId>node</artifactId> 
       <version>1.0-SNAPSHOT</version> 
      </dependency> 

Los archivos jar tienen cada uno un archivo en/org/node/llamado resource.txt.

Esto es solo un prototipo de cómo se vería un controlador con classpath: // También tengo un resource.foo.txt en mis recursos locales para este proyecto.

Los levanta y los imprime.

 
    

    package com.foo; 

    import java.io.File; 
    import java.io.FileReader; 
    import java.io.InputStreamReader; 
    import java.io.Reader; 
    import java.net.URI; 
    import java.net.URL; 
    import java.util.Enumeration; 
    import java.util.zip.ZipEntry; 
    import java.util.zip.ZipFile; 

    /** 
    * Prototype resource reader. 
    * This prototype is devoid of error checking. 
    * 
    * 
    * I have two prototype jar files that I have setup. 
    * <pre> 
    *    <dependency> 
    *     <groupId>invoke</groupId> 
    *     <artifactId>invoke</artifactId> 
    *     <version>1.0-SNAPSHOT</version> 
    *    </dependency> 
    * 
    *    <dependency> 
    *     <groupId>node</groupId> 
    *     <artifactId>node</artifactId> 
    *     <version>1.0-SNAPSHOT</version> 
    *    </dependency> 
    * </pre> 
    * The jar files each have a file under /org/node/ called resource.txt. 
    * <br /> 
    * This is just a prototype of what a handler would look like with classpath:// 
    * I also have a resource.foo.txt in my local resources for this project. 
    * <br /> 
    */ 
    public class ClasspathReader { 

     public static void main(String[] args) throws Exception { 

      /* This project includes two jar files that each have a resource located 
       in /org/node/ called resource.txt. 
      */ 


      /* 
       Name space is just a device I am using to see if a file in a dir 
       starts with a name space. Think of namespace like a file extension 
       but it is the start of the file not the end. 
      */ 
      String namespace = "resource"; 

      //someResource is classpath. 
      String someResource = args.length > 0 ? args[0] : 
        //"classpath:///org/node/resource.txt"; It works with files 
        "classpath:///org/node/";     //It also works with directories 

      URI someResourceURI = URI.create(someResource); 

      System.out.println("URI of resource = " + someResourceURI); 

      someResource = someResourceURI.getPath(); 

      System.out.println("PATH of resource =" + someResource); 

      boolean isDir = !someResource.endsWith(".txt"); 


      /** Classpath resource can never really start with a starting slash. 
      * Logically they do, but in reality you have to strip it. 
      * This is a known behavior of classpath resources. 
      * It works with a slash unless the resource is in a jar file. 
      * Bottom line, by stripping it, it always works. 
      */ 
      if (someResource.startsWith("/")) { 
       someResource = someResource.substring(1); 
      } 

       /* Use the ClassLoader to lookup all resources that have this name. 
       Look for all resources that match the location we are looking for. */ 
      Enumeration resources = null; 

      /* Check the context classloader first. Always use this if available. */ 
      try { 
       resources = 
        Thread.currentThread().getContextClassLoader().getResources(someResource); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 

      if (resources == null || !resources.hasMoreElements()) { 
       resources = ClasspathReader.class.getClassLoader().getResources(someResource); 
      } 

      //Now iterate over the URLs of the resources from the classpath 
      while (resources.hasMoreElements()) { 
       URL resource = resources.nextElement(); 


       /* if the resource is a file, it just means that we can use normal mechanism 
        to scan the directory. 
       */ 
       if (resource.getProtocol().equals("file")) { 
        //if it is a file then we can handle it the normal way. 
        handleFile(resource, namespace); 
        continue; 
       } 

       System.out.println("Resource " + resource); 

       /* 

       Split up the string that looks like this: 
       jar:file:/Users/rick/.m2/repository/invoke/invoke/1.0-SNAPSHOT/invoke-1.0-SNAPSHOT.jar!/org/node/ 
       into 
        this /Users/rick/.m2/repository/invoke/invoke/1.0-SNAPSHOT/invoke-1.0-SNAPSHOT.jar 
       and this 
        /org/node/ 
       */ 
       String[] split = resource.toString().split(":"); 
       String[] split2 = split[2].split("!"); 
       String zipFileName = split2[0]; 
       String sresource = split2[1]; 

       System.out.printf("After split zip file name = %s," + 
         " \nresource in zip %s \n", zipFileName, sresource); 


       /* Open up the zip file. */ 
       ZipFile zipFile = new ZipFile(zipFileName); 


       /* Iterate through the entries. */ 
       Enumeration entries = zipFile.entries(); 

       while (entries.hasMoreElements()) { 
        ZipEntry entry = entries.nextElement(); 
        /* If it is a directory, then skip it. */ 
        if (entry.isDirectory()) { 
         continue; 
        } 

        String entryName = entry.getName(); 
        System.out.printf("zip entry name %s \n", entryName); 

        /* If it does not start with our someResource String 
         then it is not our resource so continue. 
        */ 
        if (!entryName.startsWith(someResource)) { 
         continue; 
        } 


        /* the fileName part from the entry name. 
        * where /foo/bar/foo/bee/bar.txt, bar.txt is the file 
        */ 
        String fileName = entryName.substring(entryName.lastIndexOf("/") + 1); 
        System.out.printf("fileName %s \n", fileName); 

        /* See if the file starts with our namespace and ends with our extension.   
        */ 
        if (fileName.startsWith(namespace) && fileName.endsWith(".txt")) { 


         /* If you found the file, print out 
          the contents fo the file to System.out.*/ 
         try (Reader reader = new InputStreamReader(zipFile.getInputStream(entry))) { 
          StringBuilder builder = new StringBuilder(); 
          int ch = 0; 
          while ((ch = reader.read()) != -1) { 
           builder.append((char) ch); 

          } 
          System.out.printf("zip fileName = %s\n\n####\n contents of file %s\n###\n", entryName, builder); 
         } catch (Exception ex) { 
          ex.printStackTrace(); 
         } 
        } 

        //use the entry to see if it's the file '1.txt' 
        //Read from the byte using file.getInputStream(entry) 
       } 

      } 


     } 

     /** 
     * The file was on the file system not a zip file, 
     * this is here for completeness for this example. 
     * otherwise. 
     * 
     * @param resource 
     * @param namespace 
     * @throws Exception 
     */ 
     private static void handleFile(URL resource, String namespace) throws Exception { 
      System.out.println("Handle this resource as a file " + resource); 
      URI uri = resource.toURI(); 
      File file = new File(uri.getPath()); 


      if (file.isDirectory()) { 
       for (File childFile : file.listFiles()) { 
        if (childFile.isDirectory()) { 
         continue; 
        } 
        String fileName = childFile.getName(); 
        if (fileName.startsWith(namespace) && fileName.endsWith("txt")) { 

         try (FileReader reader = new FileReader(childFile)) { 
          StringBuilder builder = new StringBuilder(); 
          int ch = 0; 
          while ((ch = reader.read()) != -1) { 
           builder.append((char) ch); 

          } 
          System.out.printf("fileName = %s\n\n####\n contents of file %s\n###\n", childFile, builder); 
         } catch (Exception ex) { 
          ex.printStackTrace(); 
         } 

        } 

       } 
      } else { 
       String fileName = file.getName(); 
       if (fileName.startsWith(namespace) && fileName.endsWith("txt")) { 

        try (FileReader reader = new FileReader(file)) { 
         StringBuilder builder = new StringBuilder(); 
         int ch = 0; 
         while ((ch = reader.read()) != -1) { 
          builder.append((char) ch); 

         } 
         System.out.printf("fileName = %s\n\n####\n contents of file %s\n###\n", fileName, builder); 
        } catch (Exception ex) { 
         ex.printStackTrace(); 
        } 

       } 

      } 
     } 

    } 


 

You can see a fuller example here with the sample output.

Cuestiones relacionadas