0

I have a swing project structure in eclipse like the following

MyProject
  --src //the java source files
  --CustomFolder
    -abc.txt
    -def.bat

I am accessing abc.txt and def.bat using

File theFile = new File("CustomFolder\abc.txt");

When I run this in eclipse its working fine. But when I export JAR of the project and execute using JAR then it is throwing exception of FileNotFound. I tried using following as well from this link

URL fileUrl = getClass().getClassLoader().getResource("CustomFolder\abc.txt");

but the fileUrl is coming as NULL. So my question is how to provide a file path so that it could be accessed when created a JAR or after creating an exe of the JAR.

Karan
  • 752
  • 2
  • 13
  • 34
  • Can you please verify that the files are contained within your Jar file? You can open the Jar file with any archive program that supports ZIP compression (e.g. change the Jar file ending .jar to .zip and extract it). – fireandfuel Jun 05 '20 at 20:55
  • Yes they are in JAR as I extracted JAR to see the content. – Karan Jun 05 '20 at 20:57
  • I guess the answer for your question is already there: [Accessing files inside a jar file](https://stackoverflow.com/questions/30904293/accessing-files-inside-a-jar-file) – fireandfuel Jun 05 '20 at 20:58
  • I linked same question – Karan Jun 05 '20 at 20:59
  • Do you have read the answer to this question carefully? – fireandfuel Jun 05 '20 at 21:02
  • What I missed? I think the answer asking to use getClassLoader – Karan Jun 05 '20 at 21:03
  • 1
    i don't want to sound stupid but why don't put this folder inside resource? You are supposed to put anything which should be used as resource inside that folder – Snix Jun 05 '20 at 21:09
  • The path you use is wrong, you have to use `/CustomFolder/abc.txt`. `CustomFolder` is your package. `.getClassLoader()` is usually not needed. – fireandfuel Jun 05 '20 at 21:10
  • Okay I will try this as well and let you know. @Snix I will try that as well. – Karan Jun 05 '20 at 21:15
  • @Karan, I ran into a similar issue, did you find a solution? or did you find any alternative approach? – M S Kulkarni Aug 24 '22 at 15:07

0 Answers0