Get the "root" of an applicationTag(s): IO


By root, we mean the starting directory.
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class DummyApp {
  public static void main(String args[]) throws Exception{
    DummyApp s = new DummyApp();
    s.getRoot();
  }

  public void getRoot() throws IOException, URISyntaxException{
    URL u = getClass().getProtectionDomain().getCodeSource().getLocation();
    File f = new File(u.toURI());
    System.out.println(f.getParent());
  }
}
Running from a regular class
C:\Applications\dev\HowTo>java -cp . DummyApp
C:\Applications\dev
Running from a jar
C:\Applications\dev\HowTo>jar cf DummyApp.jar DummyApp.*

C:\Applications\dev\HowTo>java -cp DummyApp.jar DummyApp
C:\Applications\dev\HowTo
See these related HowTo's : 1  2
blog comments powered by Disqus