Java workspace and file path

It's probably easy for me to solve the problem. I have a folder in my project and want to get it using the relative path:

new File("/folder") 

this gives me a FileNotFoundException

if i try like this

 new File("d:/workspace/project/folder") 

works

I suspect this does not work because of this: new File("").getAbsolutePath() returns: D:\eclipse Therefore, not the path to the workspace.

Am I doing something wrong or do I need to change some settings in eclipse

+4
source share
7 answers

Just found my answer in Run Cofigurations, as djna suggested, but not on the Environment tab, but on the Arguments tab. There is a working directory section in which d: \ eclipse is installed and for which you need to set the value $ {workspace_loc: myproject}

+4
source

Don pointed out that you are using the absolute path. This is problem.

By default, the working directory in Eclipse is the path to the project where your program is running.

You can always find out the working directory by specifying this property:

 System.out.println(System.getProperty("user.dir")); 
+3
source
 new File("/folder") 

is not a relative path, it is absolute. If you want to access relative path usage

 new File("folder") 

or

 new File("./folder") 
+2
source

Is it possible to try?

 File file = new File("folder"); String path = file.getAbsolutePath(); FileInputStream fis = new FileInputStream(path); 
+1
source

In startup configurations, you can specify the working directory to run. This may vary in different eclipses, but for me:

 Run->Run Cofigurations ... Select Java Application, right Click New Environment Tab gives you a chance to specify the working directory Select Other, and then Workspace ... to specify a project in your workspace 
+1
source

D:\eclipse is the root folder of your project. If you create another project in D:\java in the same workspace, new File("").getAbsolutePath() will return D:\java so that it does not depend on the path of your workspace.

If you need a folder inside your project, use . , eg:

 System.out.println(new File(".\\test").getAbsolutePath()); 
0
source

If you want to create a folder, you need to call mkdir () on the File object. It will create a folder inside your project directory.

0
source

All Articles