Results 1 to 9 of 9
- 04-29-2011, 12:23 AM #1
Command to return the path the class is curently in.
Hello all.
This is my first post.
I need to be able to return the path the class is running from.
This is what I have come up with:
This appears to work, does everyone agree with what I have done.Java Code:import java.util.*; import java.io.*; public class ReturnPath { public static void main( String[] args ) { ReturnPath m = new ReturnPath(); m.getPath(); } public void getPath() { File f = new File( "" ); String pathName = f.getAbsolutePath(); System.out.println( pathName ); } }
- 04-29-2011, 01:32 AM #2
Another way is:
Java Code:System.getProperty("user.dir");Last edited by ra4king; 04-29-2011 at 06:29 AM.
- 04-29-2011, 06:19 AM #3
No, it'll give the path of the current working directory.That will give the path to the folder/package of that specific class.
No, that'll give the path to the user's home directory, which has nothing to do with where the .class file is located.If you want the path to the folder/package of the main program class, you would do:
Java Code:System.getProperty("user.dir");
db
- 04-29-2011, 06:28 AM #4
@Darryl.Burke
No "user.dir" returns the "User's current working directory".
"user.home" returns the user's home directorr.
System (Java Platform SE 6))
But you are quite correct on the first statement. I got confused with something else, the OP's way works too.
- 04-29-2011, 06:33 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Of "user.dir":
Umm, well, ...
@OP: See for yourself. Compile and run this all over the place to see what these things give you.
[Edit] slow ;( But it is a good idea to try these things out, follow up on the API docs etc.Java Code:import java.io.File; public class Test { public static void main(String[] args) { System.out.println(new File("").getAbsolutePath()); System.out.println(System.getProperty("user.home")); System.out.println(System.getProperty("user.dir")); } }Last edited by pbrockway2; 04-29-2011 at 06:36 AM.
- 04-29-2011, 06:35 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 04-29-2011, 07:32 AM #7
- 04-29-2011, 08:43 AM #8
I ran the following program:
Here is the output:PHP Code:import java.util.*; import java.io.*; public class ReturnPath { public static void main( String[] args ) { ReturnPath m = new ReturnPath(); m.getPath(); } public void getPath() { File f = new File( "" ); String pathName = f.getAbsolutePath(); System.out.println( pathName ); System.out.println( System.getProperty( "user.home" ) ); System.out.println( System.getProperty( "user.dir" ) ); } }
Apparently System.getProperty( "user.dir" ) will give me what I want.Java Code:/media/storage/java_progs /home/bob /media/storage/java_progs
Thanks all.
- 04-30-2011, 04:46 AM #9
Similar Threads
-
Finding path to my command line tools from Java
By paulanon in forum New To JavaReplies: 1Last Post: 04-07-2011, 02:34 AM -
How to return command prompt error message
By raxwer in forum Advanced JavaReplies: 3Last Post: 05-26-2010, 05:15 PM -
javac on the Windows command line--whitespace in path name?
By mslate in forum New To JavaReplies: 2Last Post: 03-31-2010, 12:26 AM -
want to set two path in one command
By Hussain Ali in forum New To JavaReplies: 1Last Post: 02-24-2010, 07:50 AM -
setting class-path & Library Path in ubantu
By programmer_007 in forum EclipseReplies: 18Last Post: 02-22-2010, 12:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks