PDA

View Full Version : Return path that the .class resides in.



stchman
April 29th, 2011, 12:57 AM
Hello all. I have devised a way to return the absolute path that the .class file resides in. Does everyone agree or are there better ways.



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

myrtle1908
April 29th, 2011, 01:19 AM
You could use the class loader and getResource to do that


getClass().getClassLoader().getResource("MyClass.class").toString()

stchman
April 29th, 2011, 09:04 AM
Apparently the following command:



System.getProperty( "user.dir" )


Will return the working directory.