Michele Bortolato
Michele Bortolato

Reputation: 787

A multi-platform way to access jar located resources in java

I need some code that could get an InputStream from a resource stored in some path into a jar file, this is the test code:

  String res =File.separatorChar+ "folder"+File.separatorChar+"file.txt";
  InputStream is = ReadRes.class.getResourceAsStream(res);
  System.out.println(is);

Into my jar I have the directory folder/file.txt, in linux it works but on Windows I get a null value for is . What should I do?

Upvotes: 2

Views: 350

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

Always use / when fetching the resource.

The resource is not a File, and the path is represented by an URL which always has forward slashes.

Upvotes: 4

Related Questions