Java – How to get Current Directory in Linux/Windows




Java Tutorial for Beginners
Java Tutorial for Beginners

Java – How do I get the directory that a program is running from?

In this Tutorial We will learn how to print the path of current working directory. The code below is applicable for both Linux and window.

/***********************************************************************
 * *
 * This will print a complete absolute path from where your application was initialized
 *
 * **********************************************************************
 **/

public class app {
    public static void main(String[] args) {
        System.out.println("Working Directory = " +
                System.getProperty("user.dir"));
    }
}

/*
OUTPUT
Working Directory = C:\Users\codebind\Projects\App

*/

Other way of Getting the Current Working Directory in Java is by using java.io.File

/***********************************************************************
 * *
 * This will print a complete absolute path from where your application was initialized
 *
 * **********************************************************************
 **/
import java.io.File;
public class app {
    public static void main(String[] args) {
        File currentDir = new File("");
        System.out.println("Working Directory : " + currentDir.getAbsoluteFile());
    }
}

/*
OUTPUT
Working Directory = C:\Users\codebind\Projects\App

*/

 


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*