0

My directory structure inside the compiled jar looks like this (Spring Boot Structure):

 - BOOT-INF
   - classes
      - ...
      - templates
         - Java_Template.ftl
    - lib
      - ...
 - org
   - ...
 - META-INF
   - ...

and I need to get the path of Java_Template.ftl relative to the .jar root (BOOT-INF\classes\templates\Java_template.ftl) as a String, but I don't want to hardcode the BOOT-INF\classes\ part, so how do I get it?

I've tried this.getClass().getClassLoader().getResource("templates/Java_template.ftl").toString() but it returns the path relative to C: (or some other drive), which is not what I need.

My required result: BOOT-INF\classes\
Or: BOOT-INF\classes\templates\Java_template.ftl

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Impulse The Fox
  • 2,638
  • 2
  • 27
  • 52
  • Duplicated: https://stackoverflow.com/questions/47537098/error-reading-file-in-a-jar/47537218#47537218 – Merch0 Nov 30 '17 at 11:52
  • `YourClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();` – achAmháin Nov 30 '17 at 11:58
  • @elysrivero99 What am I supposed to do with an InputStream? I need a String! – Impulse The Fox Nov 30 '17 at 12:06
  • @notyou `The method getLoc‌​ation() is undefined for the type CodeSource` :( Nevermind, you just messed up your c somehow, it was `99 226 128 140 226 128 139` in decimal – Impulse The Fox Nov 30 '17 at 12:08
  • @notyou It doesn't work, it returns `file:/C:/maven-repository/de/abc/sbk/def/0.0.1-SNAPSHOT/def-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/`. It may only have the `BOOT-INF/classes!/` part though, preferably without the exlamation marks. – Impulse The Fox Nov 30 '17 at 12:19
  • Just trying to help - it worked for me, albeit in a different scenario - that's why I added as a comment, and not an answer. – achAmháin Nov 30 '17 at 13:00
  • @ImpulseTheFox you could convert de inputStream into a String ¬¬ ... For example: https://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string – Merch0 Nov 30 '17 at 14:12
  • @elysrivero99 You got me wrong. I need the path of the file relative to the jar root (as a String). I neither need the path of the file relative to the drive, **nor the content of the file**. – Impulse The Fox Nov 30 '17 at 16:04

0 Answers0