JAVA web application, make it totally portable

0

Hello, how are you? I have a JAVA application that acts as an HTTP server, and what I would like to do is, add all the FRONT-END files of the application in the same jar, and when I transport the jar file, they can be written in the same location from where find the files of the page so when requesting these files are present on the hard drive, for example: I have the following FRONT-END files

/www/index.html
/www/jp/app.js
/www/dependencias/*.js

All these files would be compressed inside the JAR. Then when the JAR application would run, it verifies that the files are not found, the non-existent creates them. So that the program transport is simple, once it is executed, the files appear in the same directory where it is executed. Is it possible?

    
asked by Bernardo Harreguy 21.11.2017 в 17:44
source

1 answer

1

His idea is interesting and there are several ways to implement it.

If I had this problem and it was not too many files, I would not extract the resources from the jar, but it would serve them directly from within the jar and send them in the responses to the requests.

You can also extract the resources to a temporary folder where the application has write permission, preferably in a temporary location within the profile of the user who runs the application. You can implement a class that retrieves the contained resources and writes them to the disk:

CodeSource fuente = MiClase.class.getProtectionDomain().getCodeSource();
if (fuente != null) {
  URL urlJar = fuente.getLocation();
  ZipInputStream zip = new ZipInputStream(urlJar.openStream());
  while(true) {
    ZipEntry recurso = zip.getNextEntry();
    if (recurso == null)
      break; //Fin de iteracion dentro del jar
    String name = recurso.getName();
    if (/*Filtrar los recursos aqui*/...) {
      /* Si es un recurso, copiarlo a la carpeta destino */
      ...
    }
  }
} 
    
answered by 21.11.2017 / 18:08
source