3

when i create a new dynamic web project and execute the code below :

File file = new File("");
String path = file.getAbsolutePath();
System.out.println(path);

I get this result:

C:\Users\Toshiba\workspace\Projet .

which is good!!!. But when i import another project and execute the same code ,i get this :

D:\Eclipse .

(the path of eclipse's application ).

and that!! i can't explain or fix.

Any help.!

Wanna Coffee
  • 2,742
  • 7
  • 40
  • 66
simoi chigo
  • 459
  • 2
  • 7
  • 23
  • well, your grammar is bad, to be honest. but why would someone do what you're doing? why do you create File("") ? The Java API says: "Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname." Whatever that means. – Peter Butkovic Aug 19 '13 at 11:03
  • You are in a different working directory each time so you get a different result. – user207421 Aug 21 '13 at 00:59

2 Answers2

3

Because it take default location of your project ( working directory )when you pass nothing in new File("").

I have uploaded image so you can understand easily : enter image description here

Suppose your workspace path is : c:/test/myworkspace

you configure working directory as above ( in image )

and then run you get some thing like this : c:/test/myworkspace/TestJquery

Butani Vijay
  • 4,181
  • 2
  • 29
  • 61
  • please publish this solution so that i can mark it as solved . Solved!!!. go to Run --> Run Configurations... --> choose type of your project on the left --> select arguments tab on the right --> Working directory --> other: type(${workspace_loc:project_name}) . – simoi chigo Aug 19 '13 at 11:31
  • i have 2 project in my workspace when i test your code in both it display project name. i have also try using other project name as you have said means (${workspace_loc:project_name})-> here whatever project name is specified that will display. – Butani Vijay Aug 19 '13 at 11:46
3

Have you checked the documentation of the method?

If this abstract pathname is already absolute, then the pathname string is simply returned as if by the getPath() method. If this abstract pathname is the empty abstract pathname then the pathname string of the current user directory, which is named by the system property user.dir, is returned. Otherwise this pathname is resolved in a system-dependent way. On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory. On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory.

this is probably all the information you need...

Jan Hruby
  • 1,477
  • 1
  • 13
  • 26