Home > Java Get > Java Get Current Working Directory Code

Java Get Current Working Directory Code


the classpath is specificed on the command line, and the 'current directory including the package'(??) is: a. I'm sorry if my comment wasn't helpful; I sacrificed clarity to keep within the comment character limit. –Michael Scheper Jun 6 '13 at 4:17 1 This works perfectly and solves args) throws Exception { URL location = Test.class.getProtectionDomain().getCodeSource().getLocation(); System.out.println(location.getFile()); } } share|improve this answer edited Feb 1 at 16:26 community wiki 3 revs, 3 users 70%Bigoloo This is a Your program is equally useless on any system. "I'm not back." - Bill Harding, Twister Henry Wong author Marshal Posts: 22172 92 I like... http://analysedesgeeks.com/java-get/java-runtime-current-directory.html

I explore the final frontier Is there any railroad in Antarctica? The following will print out the current directory from where the command was invoked regardless where the .class or .jar file the .class file is in. FileChooser fc = new FileChooser(); fc.setInitialDirectory(new File(".")); share|improve this answer answered Jun 21 at 8:30 jpllosa 14315 add a comment| up vote -4 down vote this is current directory name String This information is stored in the user.dir system property, which you access like this: String userDir = System.getProperty("user.dir"); I needed to do this while running my project in Eclipse, because for http://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java

Java Get Project Directory

You can obtain CodeSource by ProtectionDomain#getCodeSource() and the ProtectionDomain in turn can be obtained by Class#getProtectionDomain(). public class Test { public static void main(final String[] args) { final String dir = System.getProperty("user.dir"); System.out.println("current dir = " + dir); } } if you are in /User/me/ and your I've done similar things before. Doesn't English have vowel harmony?

It is built on WordPress, hosted by Liquid Web, and the caches are served by CloudFlare CDN. How to invoke method by name in java dynamically u... Java package com.javatutorialhq.tutorial; import java.io.File; public class GetCurrentDirectory { /** * This java sample code shows * how to get the current working directory * using the property user.dir * Property Java Get Class Directory I was wrong, don't start with /. ../ also works for going up in the directory tree. –Irrationalkilla Feb 5 at 1:05 | show 1 more comment up vote 17 down

and please vote me if it useful for u –Bigoloo Aug 13 at 11:27 add a comment| up vote 11 down vote generally, as a File object: File getCwd() { return Java Set Working Directory Oct 16 at 8:47 add a comment| up vote 91 down vote The following works on Java 7 and up (see here for documentation). What exactly is a short circuit? I'm trying to get my JAR to compile, but that's a separate issue at the moment.

Some other Java tips you may like How to debug Java program in Eclipse - Java Debugging tips How to remote debug Java application in Eclipse Difference between ClassNotFoundException vs NoClassDefFoundError How To Get Current Working Directory In Java In Linux see more linked questions… Related 115Changing the current working directory in Java?704Get current stack trace in Java1How to get the present working directory on Linux in Java 0Setting the working directory Could Hashing prevent SQL injection? It's Hat Season…Announcing Winter Bash 2016 Visit Chat Linked 0 How to generically specify a file directory on a clients computer? -2 Why System.getProperty(“home.dir”) returns null 1 get App working directory

Java Set Working Directory

Hm, I think my error was more general than that - I included an extra getClassLoader() call that made it only work for classes in the default package. http://javatutorialhq.com/java/example-source-code/io/file/get-current-directory-java/ FileChooser fc = new FileChooser(); fc.setInitialDirectory(new File(".")); share|improve this answer answered Jun 21 at 8:30 jpllosa 14315 add a comment| up vote -4 down vote this is current directory name String Java Get Project Directory I really just want to know how to navigate to that folder. –Justian Meyer Jun 30 '10 at 21:06 Maybe I'm missing something but it seems like an absolute Java Get Current Directory Of Jar Basically we will explore getAbsolutePath() method of File class under java.lang package.

Find the sum of all numbers below n that are a multiple of some set of numbers Dropbox Password security What traces are left after booting by usb? this content The user.dir property is explicitly to be "User's current working directory". I want to dump a bunch of CSV files in a folder, have the program recognize all the files, then load the data and manipulate them. And the case matching problem remains. Java Get Root Directory

posted 8 years ago Your solution only works if you are in the path where the JAR file or package folder is located, since it prints out the current working directory. share|improve this answer edited Feb 5 '15 at 3:08 earcam 5,38611940 answered Sep 29 '11 at 21:12 Coder 8,17821947 4 The thread starter already tried that to no avail. –ubuntudroid Try our newsletter Sign up for our newsletter and get our top new questions delivered to your inbox (see an example). weblink Also I assumed that I should use / when creating a relative path.

If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. How To Get Folder Path In Java the OS is Windows (Use File.separator instead, or a multi-argument File constructor); 3. alvin alexander categories alaska (25) android (138) best practices (63) career (50) colorado (21) cvs (27) design (33) drupal (120) eclipse (6) funny (3) gadgets (108) git (15) intellij (4) java (429) jdbc (26) swing (74) jsp (9) latex (26) linux/unix (289) mac os x (315) mysql (54) ooa/ood (11) perl (156) php (97) postgresql (17) programming (43) ruby (56)

In Java, it's not terribly unusual for this assumption to be false.

Any idea? Oct 16 at 8:58 add a comment| up vote 25 down vote What makes you think that c:\windows\system32 is not your current directory? Paths.get() may be considered better in that it gives direct access to the more powerful Path interface. –Ole V.V. Java Working Directory Command Line Then System.getProperty("com.mycompany.workingDir")...

My guess the thread starter directly run the jar/program after starting commnad prompt (which is basically at C:\WINDOWS\system32). calling createFile from within a jar Setting an Entry Point with JAR Tool FileNotFoundException When Trying to Read from a .properties file How to call a stand alone class from WAS??? Oct 16 at 8:56 What is the potential advantage of using .normalize() in this context? –Ole V.V. http://analysedesgeeks.com/java-get/java-get-working-directory.html Here is what did work: java.nio.file.Paths.get( getClass().getProtectionDomain().getCodeSource().getLocation().toURI() ); Edit: The final version in my code: URL myURL = getClass().getProtectionDomain().getCodeSource().getLocation(); java.net.URI myURI = null; try { myURI = myURL.toURI(); } catch (URISyntaxException

more stack exchange communities company blog Stack Exchange Inbox Reputation and Badges sign up log in tour help Tour Start here for a quick overview of the site Help Center Detailed Java Program to find factorial of number in Java -... Difference between "raise" and "lift" Help, my office wants infinite branch merges as policy; what other options do we have? This is probably what you want.

To get the actual path to the application's main folder see my answer below. –Peter De Winter May 15 '13 at 14:31 The problem with the "user.dir" property is, Hard-Coding is not an option- it needs to be adaptable if it's moved to another location. Finally, here's a link that will help you find much more information about all the system properties and environment variables you can access from your Java code. The above code works fine on my system even for a jar file in another directory - e.g. $ java -jar bar/test.jar D:\cygwin\home\jim jar:file:/D:/cygwin/home/jim/bar/test.jar!/foo/Test.class As for parsing the result, well that's

share|improve this answer edited Feb 5 '15 at 3:08 earcam 5,38611940 answered Sep 29 '11 at 21:12 Coder 8,17821947 4 The thread starter already tried that to no avail. –ubuntudroid This works fine in JAR files as well. Too bad it only works in Java 7+ :/ I need something like this for Java 6. –Spencer Doak Oct 12 '15 at 16:55 3 How is this better than public class Test { public static void main(String...

As simpler way that actually works would be System.out.println(new File("").getAbsolutePath()); or System.out.println(System.getProperty("user.dir")); However, that's not what Prashant asked. What is interface in Java with Example - Tutorial How to find current directory in Java with Example... ► March ( 28 ) ► February ( 18 ) ► January (