0

I am writing a small Java Library (say project A) to be used externally (as a .JAR) in any other project (project B).

This is how project A looks like :

projectA
--src/main/java
  --packageOne
    ....
  --packageTwo
    --A.java // need to access the next few text files in this java file
    --ImportantTextOne.txt
    --ImportantTextTwo.txt
    --ImportantTextThree.txt

This is how project B will look like :

projectB
--src/main/java
  --B.java // I will use project A here.

I have tried importing the text files, but every time I use it as a .JAR externally in ProjectB, I always get some errors such as

java.nio.file.NoSuchFileException

I presume this is because of some class path issue.

So how do i correctly read my text files in ProjectA?

thanks in advance

Edit : I don't need the text files in projectB, they are just used once to pull text from in projectA. All I want is to correctly read those files in projectA, so I can import projectA in any project and not get errors.

Aditya
  • 1
  • 4

2 Answers2

0

You placed the txt files besides the class files. You may have to move them to src/main/resources/packageTwo for your build process to correctly handle them. Anyway, make sure they are at the right location once the jar file is built.

To access such a file, you cannot load it from the filesystem as it is part of your jar. But it is on the classpath. So you need to access it like

URL url = A.class.getResource("/packageTwo/ImportantTextOne.txt");
// check what url you got - if it is null the resource was not found
InputStream in = url.openStream();
...
Queeg
  • 7,748
  • 1
  • 16
  • 42
  • So this works when I use ProjectA standalone ( I printed both the url and the file contents). However when I use it as an external .JAR file in ProjectB, url is null. – Aditya Jun 27 '22 at 19:40
  • Then the txt files are likely not part of your jar or in the wrong location. The jar is just a ZIP archive. Open it and check yourself. If they are missing, fix ProjectA. – Queeg Jun 28 '22 at 06:30
  • I changed `A.class.getResource()` to `this.class.getResource()` Also what I was doing wrong was I was trying to read it as a file, rather than using InputStream. When I made the changes, viola, it works ! I'll add my own answer to clarify more, but thanks @hiran-chaudhuri – Aditya Jun 28 '22 at 08:44
0

So with the help of Hiran's response and digging around (also this) I figured it out.

File structure of the library you are writing :

projectA
--src/main/java
  --packageOne
    ....
  --packageTwo
    --A.java // need to access the next few text files in this java file
    --ImportantTextOne.txt
    --ImportantTextTwo.txt
    --ImportantTextThree.txt

Whenever reading a file, treat it as a resource. As when the class will be used as an external .JAR you will not be able to access files inside the .JAR. Instead follow this type of pattern :

A.java

URL url = this.getClass().getResource("ImportantTextOne.txt") // this assumes your text file is in the same location as A.java
InputStream in = url.openStream();
String text = new BufferedReader(
    new InputStreamReader(in, StandardCharsets.UTF_8))
    .lines()
    .collect(Collectors.joining("\n"));
Aditya
  • 1
  • 4