2

I want to access file on my classpath called reports/invoiceSweetChoice.jasper in jar on production server. Whatever I do I get null.

I have tried this.getClass().getResourceAsStream("reports/invoiceSweetChoice.jasper"), tried via InputStream etc. I have printed out content of System.getProperty("java.class.path") and it is empty. Not sure how is that possible. Do you have any suggestion how to resolve this ?

6
  • It seems to be different for jars. Does this help (section 5)? mkyong.com/java/java-read-a-file-from-resources-folder Commented Nov 22, 2020 at 22:08
  • In your jar's manifest.mf, what's your classpath? Commented Nov 22, 2020 at 22:09
  • Have a look at the spring ResourceLoader interface. You can autowire it. And you probably want to put the file in the src/main/resources directory. Commented Nov 22, 2020 at 22:15
  • @HopeyOne file is in src/main/resources/reports/ folder. Could you please explain what did you thought with ResourceLoader class ? Commented Nov 22, 2020 at 22:19
  • @hd1 manifest does not have that info. How to define it ? Commented Nov 22, 2020 at 22:20

2 Answers 2

2

In manifest.mf, the classpath is defined using the class-path key and a space-delimited list of files, as follows:

MANIFEST.MF at root of jarfile.

Class-Path: hd1.jar path/to/label007.jar path/to/foo.jar 

If there are spaces in the jar filename, you should enclose them in quotes.

If it's a webapp, the reports path should be in your BOOT-INF subdirectory of your classpath -- this is automatically performed by maven if you put it in src/main/resources in the standard layout.

EDIT:

Now that you've clarified what you're trying to do, you have 2 approaches. Like I said above, you can grab the file from the BOOT-INF subdirectory of your webapp or you can enumerate the entries in the jar until you find the one you want:

JarInputStream is = new JarInputStream(new FileInputStream("your/jar/file.jar")); JarEntry obj = null while ((obj = is.getNextJarEntry()) != null) { JarEntry entry = (JarEntry)obj; if (entry.getName().equals("file.abc")) { ByteArrayOutputStream baos = new ByetArrayOutputStream(); IOUtils.copy(jarFile.getInputStream(entry), baos); String contents = new String(baos.toByteArray(), "utf-8"); // your entry is now read into contents } } 
Sign up to request clarification or add additional context in comments.

9 Comments

this is when reading from some other jar? But this file is in jar that I'm running. If jar is named test.jar do I need to add Class-Path : test.jar even if MANIFEST.MF and the file that I want to read are in that jar ?
Only if they are in external jars. If the class is included in the jar, you needn't. If it's in an embedded jar, you will need a custom classloader.
File is in the jar. Hmm...I need a custom classloader? How to do that? And why do I need it, why it does not load in normal way ?
Which jar? The one you're running or is it embedded within the jar?
File is in jar that I'm running.
|
2
@Autowired private ResourceLoader resLoad; void someMethod() { Resource r = resLoad.getResource("classpath:reports/file.abc"); r.getInputStream()... ... 

5 Comments

I got java.lang.NullPointerException. And I have checked, I got my file in that folder
Does the reports directory with the file exist in the jar? Should be in BOOT-INF/classes/
Yes, it's there. This is a web app if that matters. And I'm running it with java -jar jarFile.jar. Am I missing some weird server configuration ?
I can't find any specific configuration to get this to work in our spring boot jar. There's no classpath in our manifest.
What version of spring-boot are you running?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.