How to get the absolute path?

J

John

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.
 
J

John

Morten Alver said:
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/tutorial/essential/system/properties.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.
 
H

hilz

John said:
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();
}
 
T

Tor Iver Wilhelmsen

John said:
"this" means the absolute path of the file "TestApp.class".

That would be the URL returned by
TestApp.class.getResource("TestApp.class");
 
Joined
Apr 5, 2011
Messages
1
Reaction score
0
"John" <[email protected]> wrote in message news:[email protected]...
> 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top