score:0

i've just used this :

system.out.println(system.getenv().get("pwd"));

using openjdk 11

score:2

file currdir = new file(".");
string path = currdir.getabsolutepath();
system.out.println(path);

this will print . at the end. to remove, simply truncate the string by one char e.g.:

file currdir = new file(".");
string path = currdir.getabsolutepath();
path = path.substring(0, path.length()-1);
system.out.println(path);

score:3

you can get the current project path use system.getproperty("user.dir")

in that method you write key name to get your different-different paths, and if you don't know key, you can find all property use of system.getproperties() this method is return all property with key. and you can find key name manually from it.

and write system.getproperty("key name")

and get your require path.

score:3

this is a code snippet to retrieve the path of the current running web application project in java.

public string getpath() throws unsupportedencodingexception {

    string path = this.getclass().getclassloader().getresource("").getpath();
    string fullpath = urldecoder.decode(path, "utf-8");
    string patharr[] = fullpath.split("/web-inf/classes/");
    system.out.println(fullpath);
    system.out.println(patharr[0]);
    fullpath = patharr[0];

    return fullpath;

}

source: https://dzone.com/articles/get-current-web-application

score: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.

score:8

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.

score:13

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.");
    }
}

score:37

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

score:130

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.


Related Query

More Query from same tag