// Tutorial //

How to open a File in Java

Published on August 3, 2022
Default avatar
By Pankaj
Developer and author at DigitalOcean.
How to open a File in Java

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

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. JavaOpenFile.java

package com.journaldev.files;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

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();
        if(file.exists()) desktop.open(file);
        //let's try to open PDF file
        file = new File("/Users/pankaj/java.pdf");
        if(file.exists()) desktop.open(file);


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 java.io.IOException. That’s all for a simple program to open a file in java.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar


Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?

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

- Jeffrey

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

    - Mansi Sharma

      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

      - monsur

        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

        - Dinesh

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

          - nadim kazi

            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

            - kesav

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

              - Mali

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

                - Ganesh

                  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.

                  - manjay.kumar

                    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.

                    - dio