Advertisement
cyecize

Config Properties hack

Apr 4th, 2022
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. try {
  2.             final String jarPath = URLDecoder.decode(
  3.                     this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile(),
  4.                     "UTF-8"
  5.             );
  6.  
  7.             final String filePath = System.getProperty("catalina.base")
  8.                     + System.getProperty("file.separator")
  9.                     + "configurations"
  10.                     + System.getProperty("file.separator")
  11.                     + "config.properties";
  12.  
  13.             final File cfgFile = new File(filePath);
  14.             cfgFile.getParentFile().mkdirs();
  15.             cfgFile.createNewFile();
  16.  
  17.             final JarFile jarFile = new JarFile(new File(jarPath));
  18.             final Enumeration<JarEntry> entries = jarFile.entries();
  19.  
  20.             while (entries.hasMoreElements()) {
  21.                 final JarEntry jarEntry = entries.nextElement();
  22.                 if (jarEntry.getName().equals("config.properties")) {
  23.                     try (InputStream in = jarFile.getInputStream(jarEntry)) {
  24.                         try (OutputStream out = new FileOutputStream(cfgFile)) {
  25.                             byte[] buf = new byte[2048];
  26.  
  27.                             for(int bytesRead = in.read(buf); bytesRead != -1; bytesRead = in.read(buf)) {
  28.                                 out.write(buf, 0, bytesRead);
  29.                             }
  30.  
  31.                             out.flush();
  32.                         }
  33.                     }
  34.                     break;
  35.                 }
  36.             }
  37.         } catch (IOException ex) {
  38.             ex.printStackTrace();
  39.             throw new RuntimeException(ex);
  40.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement