Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to get the absolute path?

Reply
Thread Tools

How to get the absolute path?

 
 
John
Guest
Posts: n/a
 
      08-01-2004
Hi.

I wrote the codes bellow.

<example>

public class TestApp {

public static void main(String[] args) {

// Question : How to get the absolute path of "this" ?

}
}

</example>

Thank for your answer to the question above.


 
Reply With Quote
 
 
 
 
Morten Alver
Guest
Posts: n/a
 
      08-01-2004
> public class TestApp {
>
> public static void main(String[] args) {
>
> // Question : How to get the absolute path of "this" ?
>
> }
> }


What do you mean by 'the absolute path of "this"'? If you mean the
current working directory, you can use System.getProperty(). See this page:

http://java.sun.com/docs/books/tutor...roperties.html


--
Morten
 
Reply With Quote
 
 
 
 
John
Guest
Posts: n/a
 
      08-02-2004
"Morten Alver" <morten@no_spam.boblefisk.org> wrote in message
news:410d6936$(E-Mail Removed)...
> > public class TestApp {
> >
> > public static void main(String[] args) {
> >
> > // Question : How to get the absolute path of "this" ?
> >
> > }
> > }

>
> What do you mean by 'the absolute path of "this"'? If you mean the
> current working directory, you can use System.getProperty(). See this

page:
>
> http://java.sun.com/docs/books/tutor...roperties.html
>


Thank you for your reply.
"this" means the absolute path of the file "TestApp.class".
System.getProperty("user.dir") don't return the path.
For example ( I use the Eclipse platform),

<example>

<directory>

c:\eclipse
| - workspace
| - TestAppProject
| - TestAppPackage
| -
TestApp.class
| -
TestApp.java


</directory>

<codes>

public class TestApp {

public static void main(String[] args) {

String path = System.getProperty("user.dir");
System.out.print(path);
}
}

</codes>

</example>

In the example above, System.getProperty() returns the path
"c:\eclipse\workspace\TestAppProject".
Is there the easy and simple way to get the absolute path of the file
"TestApp.class" ?

Thanks.



 
Reply With Quote
 
hilz
Guest
Posts: n/a
 
      08-02-2004

"John" <(E-Mail Removed)> wrote in message news:cej8ui$gmc$(E-Mail Removed)...
> Hi.
>
> I wrote the codes bellow.
>
> <example>
>
> public class TestApp {
>
> public static void main(String[] args) {
>
> // Question : How to get the absolute path of "this" ?
>
> }
> }
>
> </example>
>
> Thank for your answer to the question above.
>
>



could it be this:

String getAbsolutePath(){
java.security.ProtectionDomain pd =
YourClassName.class.getProtectionDomain();
if ( pd == null ) return null;
java.security.CodeSource cs = pd.getCodeSource();
if ( cs == null ) return null;
java.net.URL url = cs.getLocation();
if ( url == null ) return null;
java.io.File f = new File( url.getFile() );
if (f == null) return null;

return f.getAbsolutePath();
}




 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      08-02-2004
"John" <(E-Mail Removed)> writes:

> "this" means the absolute path of the file "TestApp.class".


That would be the URL returned by
TestApp.class.getResource("TestApp.class");
 
Reply With Quote
 
mukeshkoshym mukeshkoshym is offline
Junior Member
Join Date: Apr 2011
Posts: 1
 
      04-05-2011
Quote:
Originally Posted by hilz View Post
"John" <(E-Mail Removed)> wrote in message news:cej8ui$gmc$(E-Mail Removed)...
> Hi.
>
> I wrote the codes bellow.
>
> <example>
>
> public class TestApp {
>
> public static void main(String[] args) {
>
> // Question : How to get the absolute path of "this" ?
>
> }
> }
>
> </example>
>
> Thank for your answer to the question above.
>
>



could it be this:

String getAbsolutePath(){
java.security.ProtectionDomain pd =
YourClassName.class.getProtectionDomain();
if ( pd == null ) return null;
java.security.CodeSource cs = pd.getCodeSource();
if ( cs == null ) return null;
java.net.URL url = cs.getLocation();
if ( url == null ) return null;
java.io.File f = new File( url.getFile() );
if (f == null) return null;

return f.getAbsolutePath();
}
This really works. Thanks for the information.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How does one get an absolute absolute file path? James Byrne Ruby 3 09-14-2010 06:02 PM
get the max "absolute" integer in a vector JDT C++ 10 03-14-2007 09:40 PM
How to get the absolute path of the library currently loaded? bruno.fischel@gmail.com C++ 4 05-03-2006 08:17 AM
get the absolute url from JSP or servlet Ashraf Fouad Java 0 08-23-2004 02:23 PM
Get Absolute Url of FileSystem file nfalconer ASP .Net 0 07-22-2003 07:32 PM



Advertisments