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.

4
  • 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 Commented Jun 8, 2018 at 23:33
  • This might very well be a duplicate of stackoverflow.com/questions/16570523/… Commented Jun 8, 2018 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. Commented Jun 8, 2018 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 minimal reproducible example or Short, Self Contained, Correct Example. Commented Jun 9, 2018 at 2:10

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

2 Comments

Even with and resource files are in the jar root directory ?
I was wondering because I read it as the root of the jarfile. OP should probably add some more info to the question.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.