The system cannot find the file specified java
Tags: java, programming, computerscience
Published: 2020-12-20 11:00:25

In this article, we will discuss a common issue.When you face the following error: "java.io.FileNotFoundException: the system cannot find the file specified" and error similar to below is visible in logs:
1. Exception in thread "main" java.io.FileNotFoundException: Report.PDF (The system cannot find the file specified)

2. at java.io.FileInputStream.open(Native Method)

3. at java.io.FileInputStream.<init>(Unknown Source)

4. at java.util.Scanner.<init>(Unknown Source)

The issue as the error suggests is that system is not able to find the file; you need to provide the correct path for it. For example, a file can be either read on a relative path like
**<a target='_blank' href='/APP_NAME/uploads/Report.PDF'>Report</a>**

Here APP_NAME is the name of your application that contains uploads directory which contains Report.PDF. This will open a file in the browser's new tab as a PDF document.

---
title: originally posted here 👇
canonical_url: https://kodlogs.com/blog/2129/the-system-cannot-find-the-file-specified-java
---

**Please note its not a good idea to store files inside the application, as once the application is reinstalled previous files are gone. Hence you can store files outside the application and files can be rendered from there. You can store the name of the file in a database like Report.PDF and can set upload directory in System property i.e. java.io.tmpdir=D://APP_NAME/uploads/**

For example, you can create the following link to download file dynamically using JSP:

1. <a href="download.jsp?fileName=Report.PDF">Report</a> 

download.jsp should have the following code, once the above link is clicked it will be displayed as an attachment box to the end-user which he/she can either Save or Open.

**1. <%@page import="java.io.FileInputStream"%>

2. <%@page import="java.io.File"%>

3. <%@page import="com.siperian.sif.message.Record"%>

4. <%@page import="com.siperian.lookup.processor.FindTaskProcessor"%>

5.  

6. <%

7. String fileName = request.getParameter("fileName");

8.  

9. response.setContentType("application/octet-stream");

10. response.setHeader("Content-Disposition", "attachment;filename="+extractFileName(fileName));

11.  

12.  try {    

13.     

14. // You can set java.io.tmpdir in System property as "D://APP_NAME/uploads/" as its not a good idea to store files inside the application, as once the application is reinstalled previous files are gone. 

15.     

16. String filePath= System.getProperty("java.io.tmpdir");

17.     if(filePath==null || filePath.isEmpty()){

18. %> Error: Please set java.io.tmpdir. <%

19. }else{    

20. File file = new File(filePath+fileName); //"D://APP_NAME/uploads/Report.pdf"

21. FileInputStream fileIn = new FileInputStream(file);

22. ServletOutputStream output = response.getOutputStream();

23.     

24. int len = fileIn.available();

25. byte[] outputByte = new byte[len];

26. //copy binary contect to output stream

27.     while(fileIn.read(outputByte, 0, len) != -1) {

28. output.write(outputByte, 0, len);

29. }    

30. response.setContentLength((int) file.length());

31. fileIn.close();

32. output.flush();

33. output.close();

34. }

35. } catch (Exception e) {

36. %> Exception ::: <%= e.getMessage() %> <%

37. } %>

38.  

39. <%!

40.    public  String extractFileName( String filePathName ){

41.     if ( filePathName == null )  return null;

42.  

43. int slashPos = filePathName.lastIndexOf( '\\' );

44.     if ( slashPos == -1 ) slashPos = filePathName.lastIndexOf( '/' );

45.  

46.     return filePathName.substring( slashPos > 0 ? slashPos + 1 : 0 );

47. }

48. %>**

[Read more in original post](https://kodlogs.com/blog/2129/the-system-cannot-find-the-file-specified-java)