How to open a File in Java

Filed Under: Java

Sometimes we have to open a file in java program. java.awt.Desktop can be used to open a file in java. Desktop implementation is platform dependent, so first, we should check if the operating system supports Desktop or not. This class looks for the associated application registered to the current platform to open a file.

Java Open File

java open file, how to open a file in java
Let’s have a look at the simple java open file program. If we try to open a file that doesn’t exist, it will throw java.lang.IllegalArgumentException.

Let’s see Desktop class example for java open file.

package com.journaldev.files;

import java.awt.Desktop;

public class JavaOpenFile {

    public static void main(String[] args) throws IOException {
        //text file, should be opening in default text editor
        File file = new File("/Users/pankaj/source.txt");
        //first check if Desktop is supported by Platform or not
            System.out.println("Desktop is not supported");
        Desktop desktop = Desktop.getDesktop();
        //let's try to open PDF file
        file = new File("/Users/pankaj/java.pdf");


When you run the above program, the text file will be opened in the default text editor. Similarly, a PDF file will be opened in adobe acrobat reader.

If there are no application associated with given file type or the application is failed to launch, open method throws

That’s all for a simple program to open a file in java.


  1. Jeffrey says:

    Does it have to be a java file to open? Say I wanted to open Widows Mail (Microsoft email application). Would that work?

  2. Mansi Sharma says:

    Please help. “desktop is not support” msg is coming for me. I am using windows.

    1. mansi sharma says:

      and i m using jdk 13.0.2

  3. monsur says:

    how to excute a java file which is downloaded in runtime, then copied as new java file.
    the new java file will be complied under same package of the running application.

    I used threads join, wait. did not work.

    note new file was not present when application launched. it is later download. so i got file not exception. how to handle until download and copy is done. then excute it

  4. Dinesh says:

    I am not able to read/Write word file from a shard network.What to change from the above code read/Write from a shared network.

    Thanks- Dinesh

  5. nadim kazi says:

    Can anyone tell me How to open any file based on user search from D drive?
    can anyone code for it?

  6. kesav says:

    While opening Word document using above code template , my document saying

    This document contains links that may refer to other files do you want to update this document with data from the linked files ? set as true with java…..

    So how can handle that warning/alert popup using java ?
    I need that warning/alert popup and need to handle through programming .
    plz give me reply with solution as early as possible , thank you

  7. Mali says:

    Thank you very much your small piece of code make my life more flexible

  8. Ganesh says:

    This always ask to open file in read mode and locked by some one.

  9. manjay.kumar says:

    opening the files , this way –> pop up is coming with open file and ask to open in read only mode.because this is locked by some user.

  10. dio says:

    Always valid the dependencies before continue. Example, Desktop.isDesktopSupported() can fail, therefore
    instead of open file first unnecessarily, in advance you should valid all crucial conditions to your program.

  11. Ramu says:

    Thanks for the code.It is very helpful for me

  12. iffe says:

    appreciate it man .. thank you ..

  13. AYENI FEMI says:


  14. Ritesh says:

    It’s working fine. Thank you.

  15. Sidiki Traore says:

    I am trying to access a file shared in my network. It shows :

    Can someone help me to solve this problem?

    1. Manolo says:

      Perhaps the URI contains blank spaces.

  16. Shalini J says:

    Thanks for the code above, it helped me in solving my problem in opening an excel from Java code.

  17. Amaranath says:

    I am developing web project in java.I have requirement to open a pdf file in jsp.That pdf is available in web directory.Please help me

  18. Kon says:

    Amazing tutorial and straight to the point.

  19. mike says:

    Awesome! Easy to read your code and straight to the point without all the extra fluff. Bookmarking your blog.

    Thanks for the help!

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors