1

I am trying to build my own entity, which is based on VanillaWindowsProcess. The idea is, after the installation of the windows Machine, to execute some powershell commands, which are in a file.

I tried something which I used a lot of times in another Java projects to get a resource:

private void runInstallationScript() {
    List<String> lines;
    try {
        lines = FileUtils.readLines(
                new File(TalendWindowsProcessWinRmDriver.class.getResource("/my/path/file.txt").getFile()),
                "utf-8");
        executePsScript(lines);
    } catch (IOException e) {
        LOG.error("Error reading the file: ", e);
    }
}

But I'm always getting the following:

ava.io.FileNotFoundException: File 'file:/opt/workspace/incubator-brooklyn/usage/dist/target/brooklyn-dist/brooklyn/lib/dropins/myProject-0.0.1-SNAPSHOT.jar!/my/path/file.txt' does not exist

It is strange, because the file is in the jar in that path. I did a test (without Apache Brooklyn infrastructure) and it works, but the other way, it does not.

The project follows the Maven standard structure and the file itself is under, src/main/resources/my/path/file.txt

Is there something that is wrong? Or maybe there is another approach to get that file? Any help would be appreciated.

Iker Aguayo
  • 3,980
  • 4
  • 37
  • 49

1 Answers1

0

You cannot access a resource inside a jar as a File object. You need to use an InputStream (or an URL) to access it.

Since you are already using getResource, you should change the method FileUtils.readLines to accept an InputStream (or an URL) as input.

If you don't have access to the source code, you can write your own method or use Files.readAllLines for Java >= 7.

Tunaki
  • 132,869
  • 46
  • 340
  • 423