How to locate the Path of the current project directory in Java (IDE)?

276,231

Solution 1

Two ways

System.getProperty("user.dir");

or this

File currentDirFile = new File(".");
String helper = currentDirFile.getAbsolutePath();
String currentDir = helper.substring(0, helper.length() - currentDirFile.getCanonicalPath().length());//this line may need a try-catch block

The idea is to get the current folder with ".", and then fetch the absolute position to it and remove the filename from it, so from something like

/home/shark/eclipse/workspace/project/src/com/package/name/bin/Class.class

when you remove Class.class you'd get

/home/shark/eclipse/workspace/project/src/com/package/name/bin/

which is kinda what you want.

Solution 2

Use user.dir key for get project location using java.

Code:

System.out.println("Present Project Directory : "+ System.getProperty("user.dir"));

OutPut:

Present Project Directory :C:\workspace\MyEclipse 10\examples\MySqlMavenDomain

For displaying all systems properties simply use System.getProperties();

Use Key value in System.getProperty("java.version"); for get output.

Code:

Properties properties = System.getProperties();
Enumeration<Object> enumeration = properties.keys();
for (int i = 0; i < properties.size(); i++) {
    Object obj = enumeration.nextElement();
    System.out.println("Key: "+obj+"\tOutPut= "+System.getProperty(obj.toString()));
}

OutPut:

Key: java.runtime.name  OutPut= Java(TM) SE Runtime Environment
Key: sun.boot.library.path  OutPut= C:\Program Files\Java\jdk1.7.0_45\jre\bin
Key: java.vm.version    OutPut= 24.45-b08
Key: java.vm.vendor OutPut= Oracle Corporation
Key: java.vendor.url    OutPut= http://java.oracle.com/
Key: path.separator OutPut= ;
Key: java.vm.name   OutPut= Java HotSpot(TM) Client VM
Key: file.encoding.pkg  OutPut= sun.io
Key: user.country   OutPut= US
Key: user.script    OutPut= 
Key: sun.java.launcher  OutPut= SUN_STANDARD
Key: sun.os.patch.level OutPut= Service Pack 3
Key: java.vm.specification.name OutPut= Java Virtual Machine Specification
Key: user.dir   OutPut= C:\workspace\MyEclipse 10\examples\MySqlMavenDomain
Key: java.runtime.version   OutPut= 1.7.0_45-b18
Key: java.awt.graphicsenv   OutPut= sun.awt.Win32GraphicsEnvironment
Key: java.endorsed.dirs OutPut= C:\Program Files\Java\jdk1.7.0_45\jre\lib\endorsed
Key: os.arch    OutPut= x86
Key: java.io.tmpdir OutPut= C:\DOCUME~1\UDAYP~2.PBS\LOCALS~1\Temp\
Key: line.separator OutPut= 

Key: java.vm.specification.vendor   OutPut= Oracle Corporation
Key: user.variant   OutPut= 
Key: os.name    OutPut= Windows XP
Key: sun.jnu.encoding   OutPut= Cp1252
Key: java.library.path  OutPut= C:\Program Files\Java\jdk1.7.0_45\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/MyEclipse/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin/client;C:/Program Files/MyEclipse/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin;C:/Program Files/MyEclipse/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/lib/i386;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\system32\WindowsPowerShell\v1.0;D:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;D:\Program Files\Microsoft SQL Server\100\Tools\Binn\;D:\Program Files\Microsoft SQL Server\100\DTS\Binn\;.
Key: java.specification.name    OutPut= Java Platform API Specification
Key: java.class.version OutPut= 51.0
Key: sun.management.compiler    OutPut= HotSpot Client Compiler
Key: os.version OutPut= 5.1
Key: user.home  OutPut= C:\Documents and Settings\uday.p.PBSYSTEMS
Key: user.timezone  OutPut= 
Key: java.awt.printerjob    OutPut= sun.awt.windows.WPrinterJob
Key: file.encoding  OutPut= UTF-8
Key: java.specification.version OutPut= 1.7
Key: java.class.path    OutPut= C:\workspace\MyEclipse 10\examples\MySqlMavenDomain\target\test-classes;C:\workspace\MyEclipse 10\examples\MySqlMavenDomain\target\classes;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\jstl\jstl\1.2\jstl-1.2.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\mysql\mysql-connector-java\5.1.28\mysql-connector-java-5.1.28.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\javax\mail\mail\1.4.7\mail-1.4.7.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\javax\activation\activation\1.1\activation-1.1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\junit\junit\4.11\junit-4.11.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hibernate\hibernate-core\4.3.0.Final\hibernate-core-4.3.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\jboss\logging\jboss-logging\3.1.3.GA\jboss-logging-3.1.3.GA.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\jboss\logging\jboss-logging-annotations\1.2.0.Beta1\jboss-logging-annotations-1.2.0.Beta1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.2_spec\1.0.0.Final\jboss-transaction-api_1.2_spec-1.0.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hibernate\common\hibernate-commons-annotations\4.0.4.Final\hibernate-commons-annotations-4.0.4.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hibernate\javax\persistence\hibernate-jpa-2.1-api\1.0.0.Final\hibernate-jpa-2.1-api-1.0.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\javassist\javassist\3.18.1-GA\javassist-3.18.1-GA.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\jboss\jandex\1.1.0.Final\jandex-1.1.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hibernate\hibernate-entitymanager\4.3.0.Final\hibernate-entitymanager-4.3.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-dbcp\commons-dbcp\1.4\commons-dbcp-1.4.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-fileupload\commons-fileupload\1.3\commons-fileupload-1.3.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-io\commons-io\2.4\commons-io-2.4.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-net\commons-net\3.3\commons-net-3.3.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-codec\commons-codec\1.9\commons-codec-1.9.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-pool\commons-pool\1.6\commons-pool-1.6.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-beanutils\commons-beanutils\1.9.0\commons-beanutils-1.9.0.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\apache\commons\commons-lang3\3.1\commons-lang3-3.1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\log4j\log4j\1.2.16\log4j-1.2.16.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-orm\3.2.6.RELEASE\spring-orm-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-jdbc\3.2.6.RELEASE\spring-jdbc-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-tx\3.2.6.RELEASE\spring-tx-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-web\3.2.6.RELEASE\spring-web-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-aop\3.2.6.RELEASE\spring-aop-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-webmvc\3.2.6.RELEASE\spring-webmvc-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-aspects\3.2.6.RELEASE\spring-aspects-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\aspectj\aspectjweaver\1.7.2\aspectjweaver-1.7.2.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-beans\3.2.6.RELEASE\spring-beans-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-context\3.2.6.RELEASE\spring-context-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-context-support\3.2.6.RELEASE\spring-context-support-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-core\3.2.6.RELEASE\spring-core-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-expression\3.2.6.RELEASE\spring-expression-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-instrument\3.2.6.RELEASE\spring-instrument-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-instrument-tomcat\3.2.6.RELEASE\spring-instrument-tomcat-3.2.6.RELEASE.jar
Key: user.name  OutPut= uday.p
Key: java.vm.specification.version  OutPut= 1.7
Key: sun.java.command   OutPut= com.uk.mysqlmaven.domain.test.UserLoginDetails
Key: java.home  OutPut= C:\Program Files\Java\jdk1.7.0_45\jre
Key: sun.arch.data.model    OutPut= 32
Key: user.language  OutPut= en
Key: java.specification.vendor  OutPut= Oracle Corporation
Key: awt.toolkit    OutPut= sun.awt.windows.WToolkit
Key: java.vm.info   OutPut= mixed mode, sharing
Key: java.version   OutPut= 1.7.0_45
Key: java.ext.dirs  OutPut= C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
Key: sun.boot.class.path    OutPut= C:\Program Files\Java\jdk1.7.0_45\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_45\jre\classes
Key: java.vendor    OutPut= Oracle Corporation
Key: file.separator OutPut= \
Key: java.vendor.url.bug    OutPut= http://bugreport.sun.com/bugreport/
Key: sun.io.unicode.encoding    OutPut= UnicodeLittle
Key: sun.cpu.endian OutPut= little
Key: sun.desktop    OutPut= windows
Key: sun.cpu.isalist    OutPut= pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86

For more details refer Class System

Solution 3

This is the new way to do it:

Path root = FileSystems.getDefault().getPath("").toAbsolutePath();
Path filePath = Paths.get(root.toString(),"src", "main", "resources", fileName);

Or even better:

Path root = Paths.get(".").normalize().toAbsolutePath();

But I would take it one step further:

public Path getUsersProjectRootDirectory() {
    String envRootDir = System.getProperty("user.dir");
    Path rootDIr = Paths.get(".").normalize().toAbsolutePath();
    if ( rootDir.startsWith(envRootDir) ) {
        return rootDir;
    } else {
        throw new RuntimeException("Root dir not found in user directory.");
    }
}

Solution 4

What about System.getProperty("user.dir")? It'll give you the working directory from where your program was launched.

See System Properties from the Java Tutorial for an overview of Java's System Properties.

Solution 5

YOU CANT.

Java-Projects does not have ONE path! Java-Projects has multiple pathes even so one Class can have multiple locations in different classpath's in one "Project".

So if you have a calculator.jar located in your JRE/lib and one calculator.jar with the same classes on a CD: if you execute the calculator.jar the classes from the CD, the java-vm will take the classes from the JRE/lib!

This problem often comes to programmers who like to load resources deployed inside of the Project. In this case,

System.getResource("/likebutton.png") 

is taken for example.

Share:
276,231
user1765376
Author by

user1765376

Updated on July 05, 2022

Comments

  • user1765376
    user1765376 almost 2 years

    I am trying to locate the path of the current running/debugged project programmatically in Java, I looked in Google and what I found was System.getProperty("user.id"), which didn't get me the project's path.

    I know the command Environment.currentDirectory in C# gives the path of the current running/debugged project,so I am sure there must be a similar way in Java, too.

    So I am asking if anyone could tell me or give me a code to how locate the path of the currently running/debugged project programmatically?

    edit: i am writing plugin which loads with eclipse start. the plugin adds a button to the workbench of eclipse, with in the code of the plugin when i click the button i search for the current directory path.

    my purpose is that when for example i debug a project and then click the button, a window will pop up presenting the debugged project's path, thanks

  • mthmulders
    mthmulders over 11 years
    It'll print a . at the end of the path, though.
  • Yogendra Singh
    Yogendra Singh over 11 years
    @mthmulders: I was more sharing the concept. Added the substring() to truncate the . from the end.
  • user1765376
    user1765376 over 11 years
    i have already tried it, it doesnt give the current directory path, but it gives the path of the directory which contains the eclipse projects.
  • mthmulders
    mthmulders over 11 years
    What do you mean by "current directory path"? Using this code, you'll get the directory from which your program was started - and yes, Eclipse usually starts your program from the directory where your project is stored on disk.
  • user1765376
    user1765376 over 11 years
    i have edit information above, i hope it explains everything.
  • user1765376
    user1765376 over 11 years
    i have tried it. i have edit information about the subject above, hope it will explain everything a lot better,thanks.
  • mthmulders
    mthmulders over 11 years
    If you want the directory from which your debugging started, use the above. If you need something to find the projects path, you can assume it'll be equal to the directory from which debugging started - or use the Eclipse plugin API to find out. Unfortunately, I'm not familiar with any Eclipse plugin API so I can't help you on that one.
  • user1765376
    user1765376 over 11 years
    the command above does'nt help when i put that command in my plugin and try to debug a poroject, and than click the button of my plugin, thanks anyway.
  • Quatsch
    Quatsch over 8 years
    The method getResource(String) is undefined for the type System
  • Moesio
    Moesio over 6 years
    And if I want the src path not bin?
  • Rex the Strange
    Rex the Strange about 5 years
    try "getCanonicalPath ()" which will resolve relative paths
  • Dilini Peiris
    Dilini Peiris almost 5 years
    i am running my project on tomcat server. Therefore, when i use System.getProperty("user.dir") it gives me the path of the Tomcat server. Is there a way to get the path of the project folder?
  • UdayKiran Pulipati
    UdayKiran Pulipati almost 5 years
    @DiliniPeiris you may get answer in this stackoverflow.com/questions/6326228/…
  • djangofan
    djangofan almost 3 years
    getting path from classpath is different than getting it from the jvm/system
  • Marco Sulla
    Marco Sulla over 2 years
    The second point of the answer is not the answer to the question, the first one yes. Upvoted this answer: stackoverflow.com/a/22011009/1763602