-2

I'm creating a Java sdk which I ultimately want a file called version to contain a string of the current version of the sdk.

I want to read this file and pass it along in underlying API calls I make within the sdk.

Currently this file is located in src/main/resources/version and simply contains 0.1.0.

I have this method which reads the file and returns the version.

private static String getVersion() {
        URL versionFile = ConfigurationUtils.class.getResource("version");
        Path path = Paths.get(String.valueOf(versionFile));
        String version = null;
        try {
            version = Files.readAllLines(path).get(0);
        } catch (IOException e) {
            // The project should never build without if this happens so therefore just print a stacktrace
            e.printStackTrace();
        }

        return version;
    }

In another project where I'm trying to use this sdk, i'm getting a java.nio.file.NoSuchFileException: null on this line

enter image description here

I can see that the file actually does exist in my dependencies though enter image description here

Any idea what i'm missing here?

** Update **

When trying the suggestion from the possible duplicate, my sdk won't even compile b/c it's getting a null pointer exception on this line when running my tests.

enter image description here

src/main/resources/version surely exists enter image description here

Catfish
  • 18,876
  • 54
  • 209
  • 353
  • No it doesn't. I changed the code in `getVersion` to ```try (InputStream in = ConfigurationUtils.class.getResourceAsStream("version"); BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { version = reader.readLine(); ``` and my sdk project won't build. A version file keeps appearing in `src/main/java/org/catalytic/sdk/version` when I run my tests. – Catfish Jul 16 '20 at 19:16
  • For what it's worth, I bet it's faster to auto-generate a Version class with a static method that returns the version. It accomplishes the same thing, will take you a few minutes, be smaller and cleaner, and significantly faster at runtime. – Joseph Larson Jul 16 '20 at 19:19
  • Auto generate it from what? when would it be auto generated? – Catfish Jul 16 '20 at 19:21
  • @Catfish When your project doesn't get build you have to check for compile errors. Keep in mind that `Files.readAllLines()` read from a file, but the `version` entry is inside a `jar`. Please edit your question to include the source code and error messages you get, when you try to read the file as mentioned in the duplicate. – Progman Jul 16 '20 at 19:22
  • @Progman I've updated my question with the latest attempt from the possible duplicate suggestion. – Catfish Jul 16 '20 at 19:30
  • @Catfish Please edit your question to include a [mcve]. Also add to your question how you execute your "SDK", how you built your `jar` file and how you set your classpath when running your java application. – Progman Jul 16 '20 at 19:42
  • You should use `"/version"`, not `"version"`, and you should not try to convert to a path. Instead, use `getResourceAsStream`. – Mark Rotteveel Jul 18 '20 at 16:03

1 Answers1

0

You can't read the resource from the Jar because you're trying to read it as a file in which it is not. Instead, what you should be doing is reading all of the lines of the InputStream using a BufferedReader.

    private static String getVersion() throws IOException {
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(ConfigurationUtils.class.getResourceAsStream("version")))) {
            return reader.lines()
                    .findFirst()
                    .orElseThrow(IllegalStateException::new);
        }
    }
Jason
  • 5,154
  • 2
  • 12
  • 22
  • I think I figured it out. I was missing `getClassLoader()`. So `ConfigurationUtils.class.getResourceAsStream("version");` should be `ConfigurationUtils.class.getClassLoader().getResourceAsStream("version");` – Catfish Jul 16 '20 at 19:42