Skip to content
Advertisement

How to return the file path from the windows file explorer using Java

In my Project, I want to open the windows file explorer with java, in which you can select a file or a folder and click the “OK” button. Now I want to have the path of the selected file in my Javacode.

Basically like the window which pops up in every standard texteditor after you hit the “OPEN” button to choose the file to open in the editor.

I know how to open the windows file explorer with Runtime.getRuntime().exec("explorer.exe") but I canĀ“t figure out a way to return the file path.

Advertisement

Answer

This is how I’d use a JFileChooser for your problem:

String filePath;    // File path plus name and file extension
String directory;        // File directory
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        directory = fc.getSelectedFile().getName();
    } else {
        directory = "Error in selection";
    }
filePath = directory + "\" + folderName;
Advertisement