سؤال

I have 2 property files which are part of my java eclipse project. Now i need to pass the path of these 2 files as a system property like -Dpath1="pathfile1" as project will only accept it as a system property. One option is that i can specify the absolute path but i want to specify a path relative to my project something like -Dpath1=$Project_Dir/resource/file1. Reason being, if project directory is moved then project will start failing.

I am using eclipse to run this project and dont want to declare any new system variable for supporting relative path, so for example dont want to create a new system variable Project_Dir . Is it possible to achieve this and how?

هل كانت مفيدة؟

المحلول

You can set system properties with:

System.setProperty("path1", yourPathHere);

Now, you only have to build the correct path, relative to where your project is. To do this, start off with the location of the project: How to get the path of a running JAR file?

نصائح أخرى

When using setProperty, you can reference internal project files if you use a plain relative path.

E.g. to reference a file located at /abc/def/workspace/project/lib/driver.exe:

System.setProperty("driver", "lib/driver.exe");

It's deceivingly simple.

Yes, you can do it.Eclipse have build-in Path variable PROJECT_LOC, it stores location of project folder, e.x. c:\Workspace\Project1. You can use this property in launch configuration.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top