1

I have a jar and resource files are in the jar root directory. Inside the code I have:

Kernel.class.getResourceAsStream(resource);

I start the application as

java -cp myjar.jar com.mycompany.MyClass

However, the resource is not found.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Alex
  • 7,007
  • 18
  • 69
  • 114
  • 1
    Jar files have a "class-path" manifest entry which defines the class path Java should use to find dependencies. I believe `-cp` will override this, but it's been so long since I tried anything like this I could be wrong. Besides, `-cp` is only defining a single entry, it should be listing ALL the Jar's, which why it's better to use the Jar `class-path` manifest entry – MadProgrammer Jun 08 '18 at 23:33
  • This might very well be a duplicate of https://stackoverflow.com/questions/16570523/getresourceasstream-returns-null – fvu Jun 08 '18 at 23:36
  • 3
    Please show output of `jar tvf myjar.jar` for the resource files. Also be certain you have a leading "/" in your `resource`. – Thorbjørn Ravn Andersen Jun 08 '18 at 23:42
  • `Kernel.class.getResourceAsStream(resource);` Not nearly enough information to help solve this. What is the value of `resource`? What is the listing of the Jar content (see comment of @ThorbjørnRavnAndersen)? For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 09 '18 at 02:10

2 Answers2

1

You need your current directory in your classpath.

Linux:

java -cp myjar.jar:. com.mycompany.MyClass

Windows:

java -cp myjar.jar;. com.mycompany.MyClass
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Even with `and resource files are in the jar root directory` ? – fvu Jun 08 '18 at 23:38
  • I was wondering because I read it as the root of the jarfile. OP should probably add some more info to the question. – fvu Jun 08 '18 at 23:43
0

I needed to precede the resource name by slash "/". Then finding the resource in the jar root directory works fine

Alex
  • 7,007
  • 18
  • 69
  • 114