0

I am having a little issue trying to figure out the best solution to the my path problems. I am running a java test that I want to get two things.

  1. The absolute location of the project
  2. The absolute location to the current class file that is running

I want to proper / or \ being on the OS version so the folder structure stays intact. I am currently using this but it is not exactly what I am looking for

final String parentDir = System.getProperty("user.dir");
final String path = "src/test/java/" + method.getDeclaringClass()
    .getCanonicalName().replaceAll("\\.", "/") + ".java";

Any help would be appreciated. Thanks

Update: I am trying to get the url of the precompiled code as I need access to the comments in the code. This may change some of your guys answers

Update 2: Ok I got it to work.

final String path = new File(getClass().getResource("/").getFile())
            .getParent().split("target")[0] + "src/test/java/" + method
            .getDeclaringClass().getCanonicalName()
            .replaceAll("\\.", "/") + ".java";

Thanks Guys

jrock2004
  • 2,240
  • 3
  • 28
  • 51
  • 2
    You can get the native separator have look at this link. http://stackoverflow.com/questions/8075373/file-separator-vs-filesystem-getseparator-vs-system-getpropertiesfile-separa – Elmer May 06 '13 at 19:26
  • Little improvement of your current code: use `replace('.', '/')` instead of `replaceAll("\\.", "/")` to avoid regex engine. – Pshemo May 06 '13 at 19:30
  • to get the absolute location of project try to print to the console the following: System.out.println(new File(".").getAbsolutePath()); – Mike May 06 '13 at 19:40
  • mike, tried that and this is what I get /Users/john/work/development/automation/product/. – jrock2004 May 06 '13 at 21:34

2 Answers2

1

Given that you are calling this from MyClass you should call File directory = (new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath())).getParentFile();

I had the same question once. In addition to Jatin's answer I had to add an toURI() to get the correct path on all platforms (Windows, etc.) and post 1.5 JVMs.

Christian Fries
  • 14,119
  • 9
  • 51
  • 65
  • Tried your command and I got the following /Users/john/work/development/automation/product/store/target – jrock2004 May 06 '13 at 21:37
  • Christian, lets say it works that would not really solve my problem. When I get to work tomorrow I will try on a windows machine – jrock2004 May 06 '13 at 22:06
0

If say you are running from jar file:

new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent()+"/"

returns the folder containing the jar file.

Remove the .getParent() above to get path to the exact class file

Jatin
  • 28,098
  • 11
  • 88
  • 150
  • Tried your command and I got the following /Users/john/work/development/automation/product/store/target/ – jrock2004 May 06 '13 at 21:39
  • Isn't it the path containing ur class? ALso for the project folder `new File(".");` will do. – Jatin May 07 '13 at 05:41