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.
Related
I need to generate a JAVA file which will be used by the other JAVA file within the same project.
This creation will happen while building the project. Because the class is being used within the same project, hence I think it should be placed under src/main/java only.
The issue is from Paths API, I am not able to go to this location directly. I have to hard code the absolute path to my file for creating that class.
Is there any way by which I can use a relative path to create a file?
Also , Should file be there in src folder at first place?
I am working on a java project which consumes an external jar from another project. This external jar cannot be checked-in in lib folder as it evolves continuously. So we have planned to keep the latest jar inside a folder,say 'ExtJar', under User's Home dir.
The question is how do I modify classpath to point to this jar from home dir?
So, I was looking for something like and should work for all OS platforms-
classpathentry kind="lib" path="MyHomeDir/ExtJar/myExternalJar.jar"
where MyHomeDir is a variable I defined as per the link -
- Use Eclipse classpath variable to replace absolute "sourcepath" path?
I looked into above link to add a variable but apart from this I also want this variable to point to appropriate home dir depending on OS, i.e C:\Users\\ExtJar\ for Windows OR /home//ExtJar for linux etc.
Is there any way to programatically modify value of this variable to point to home dir after evaluating which OS its being run on?
The point of a Classpath Variable is that it's value is not the same for all workspaces; each workspace defines where the variable points to.
You can still use a Classpath Variable to solve your problem, though. For example, create a variable called EXT_JAR_HOME and point it to your C:\Users\your.name\ExtJar folder. Then in the project build path, use Add Variable... to select and **Extend...* it, selecting the actual JAR file. That will result in the project's build path having an entry like EXT_JAR_HOME/ExternalJar.jar. Then each developer workspace will just need to, one time, defineEXT_JAR_HOME` and point it to the correct path. Linux users' actual location will look different than Windows users, obviously.
The point is, Classpath Variables must be defined in each workspace, that's how they're designed to work.
I would like to set the -Djava.library.path VM option to a specific folder. However, it is not relative to my project folder so that I could say for example:
-Djava.library.path=native\windows
(The folder native is in the project folder.)
Do you know if this is somehow possible to set it like above and not with -Djava.library.path=C:\...?
You actually can set a relative path. For example if you start your program a specific folder, you can access libraries in a folder "libs" right next to it by setting the path to "../libs" e.g.
In my own project with native libraries I have this in my shell script:
-Djava.library.path=../../native/unix
Hope this answers your question.
I'm not sure if you are asking how to refer to a relative directory on windows, or how to set this path without the -Djava.library.path=... parameter. So, I will answer both.
To set a relative path, use:
-Djava.library.path=.\windows
To set this path on Windows without using -D, augment the PATH environment variable:
setenv PATH %PATH%;C:\path\to\folder
On Linux/Mac, set/augment the LD_LIBRARY_PATH with this folder location.
My file is located under the src directory. However, when I try to call it using "src/readme.txt" the file is not found.
In fact, it states that java is looking for "C:\Documents and settings\john\My Documents\Downloads\eclipse-win32\eclipse\coolCarsProject\src\readme.txt".
How do I fix this? I do not want to put in the absolute path all the time.
Do I need to fix the classpath, buildpath, or change the project root, etc? It is not at all obvious from the roughly 1000 settings in Eclipse for a newbie.
First, you have to decide if you want to load the file from the file system, or if the file will in fact be bundled with your application code.
If the former, then you should really think about how your application will be launched when actually deployed, because using a relative file path means that the program should always be started from the same location: a relative path is relative to the location from where the program is started (the current directory). If this is really what you want, then edit your launch configuration in Eclipse, go to the Arguments tab, and set the working directory you want. But the src directory is not where you should put this file, since this will copy the file to the target directory, along with the classes, that will probably be put in a jar once you'll deploy the application.
If the latter, then you should not treat the file as a file, but as a resource that is loaded by the ClassLoader, using ClassLoader.getResourceAsStream() (or Class.getResourceAsStream()). Read the javadoc of those methods to understand the path to pass. If you put the file directly under src, it will be copied by Eclipse to the target directory, along with your classes, in the default package. And you should thus use SomeClass.class.getResourceAsStream("/readme.txt") to load it.
Using paths relative to the current working directory is not a good idea in general, as it's often quite hard to establish what your current working directory will be. In Eclipse, it will be your project folder (unless you set it to something different in your launch configuration), in webapps it will be the webapp's root directory, in a command line app it could be anything.
Try this one:
String filePath = ".\\userFiles\\data.json";
where «.\» is a root for the Eclipse project, «userFiles» is a folder with the user's files inside of Eclipse project. Since we are talking about Windows OS, we have to use «\» and not «/» like in Linux, but the «\» is the reserved symbol, so we have to type «\\» (double backslash) to get the desired result.
I added an external jar file into my eclipse project from the properties>libraries in my project. Then I export my project as a file system, to make runnable for every computer. However after I import to project in eclipse, jar file refers to the my computers path thats why it does not work.
How should I solve this ?
Thanks
Define a class path variable and refer to the JAR using a variable in the class path of your project. Each programmer can then set the variable according to theirs environment. Variables can be set in Window -> Preferences -> Java -> Classpath Variables and used by pressing Add Variable... in Java Build Path editor of your project. When adding a variable to classpath, you can even press Extend... and "extend" your variable with a suffix - i.e. have the variable contain a folder name and suffix it with a fixed file name.
Some more obvious options:
Copy the JAR inside your project, then export it.
Agree on a common workspace structure and use a relative path to refer to the JAR.
Use a dependency management tool (e.g. Maven).