Skip to content
Advertisement

Difference from running application in IntellJ and running JAR file

I came across a situation when i would run application in intellJ and everything runs smoothly , then i would create a jar using maven install command and try running it manually from command line, the results are the following :

Command Line

java.lang.RuntimeException: java.io.FileNotFoundException: apisrcmainresourcesFAQ.pdf (The system cannot find the path specified)
        at test.test.paisy.test.test.domain.email.FaqAttachment.pdfToBytes(FaqAttachment.java:27) ~[classes!/:0.0.1-SNAPSHOT]

Questions:

  1. What is the difference between running Jar and running App in IntelliJ
  2. Why would it complain about file path when it finds it successfully by running it in ItelliJ

Path to file

try (FileInputStream inputStream = new FileInputStream(new File("api/src/main/resources/FAQ.pdf"))) {
                this.bytes = ByteStreams.toByteArray(inputStream);

Thank you

Advertisement

Answer

First of all, if you want to use relative path, it needs to be started from the src folder, like this

new File("src/main/resources/FAQ.pdf")

But the best way is to use

InputStream inputStream = ClassName.class.getClassLoader().getResourceAsStream("FAQ.pdf"); 

So that the path won’t be relative to that static path.

Don’t forget to replace ClassName with the name of your current class

Make sure to clean the build and then rebuild before running.

For more detail take a look at this answer

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement