0

The thing is that System.getProperty("user.dir"); obtains a path without a current folder. So if class is stored in C:\Users\Nika\workspace\sint\src the output is C:\Users\Nika\workspace\sint. What should I do?

nicks
  • 2,161
  • 8
  • 49
  • 101

4 Answers4

0

I think this will help you if you want the current working directory:

Getting the Current Working Directory in Java

Community
  • 1
  • 1
ddoor
  • 5,819
  • 9
  • 34
  • 41
0

The source directory for a .java is not stored anywhere. You can obtain the based directory of the location of the .class file by getting the protection domain, however this is usally not needed.

Perhaps what you are trying to do is get a file using the class path. In this case I would suggest using ClassLoader.getResourceAsInputStream()

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Because class loader has current path to it. If yoy create jar then src path would not be valid. so append package name if classes are not in archive

Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46
0

Why not use getResource

URL url = Test.class.getClassLoader().getResource(Test.class.getName().concat(".class"));
System.out.println(url.getPath());
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72