According to https://viralpatel.net/blogs/get-eclipse-current-workspace-path/ an easy way to find the location of your current workspace is to open the Switch Workspace dialog. The location of the current workspace should be the default path there. In my case it points to ~/Eclipse workspaces, which makes sense - it was my intention to store it there. There is also a 55 MB .metadata folder there with time stamps that makes sense.
Furthermore, when I look under Run Configurations -> Arguments -> Working Directory for my current project Foo, it is set to its default value ${workspace_loc:foo}. So far so good. However, when I created a directory ABC in my program it ended up at ~/git/foo/ABC.
Admittedly, that is where my source is (according to the properties in Eclipse for my Main.java its path is ~/git/foo/src/main/java/io/zzzz/foo/Main.java).
I don't understand how Eclipse determines which directory it should use for CWD. Please enlighten me!
Edit: all other settings are at their default value.
The Run Configuration value is always used when you run your program.
The variable ${workspace_loc:foo} means the location of the contents of project foo. If your project's contents are outside of the workspace the result will be a path that is not in the workspace.
Related
The file’s getAbsolutePath has the tendency that if a relative path or just the file name is provided in the file constructor, then it will resolve the absolute path by prefixing the current working directory.
The definition of current working directory is the directory where we run our Java Program.
In my example, my java program resides inside D:\my-app\src\App.java
When I do a System.out.println(System.getProperty("user.dir"));
Then it should ideally print D:\my-app\src
But it is actually printing D:\my-app
Why is it so?
When you start an application from IntelliJ, its working directory is set according to what's set in "Working directory" in the "Run/Debug Configurations" dialog.
There, you can set the working directory to a fixed value such as D:\my-app\src. Another option is to use a path variable. The list of variables that are available apparently can depend on many things - open the dialog and see what's available. One option that I see is $FileDir$ which I imagine would be the directory of the source code file.
I think I found the solution. Intellij was actually causing the issue by implicitly setting the src folder as the source folder. I found it by going to the module setting within Intellij.
When I ran the same from a command prompt, the correct working directory i.e. D:\my-app\src\ is selected.
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.
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.
Here is my maven project structure
ProjectParent
-Class1
-Class2
-Module1 (another sub directory)
--Module1Class1
--Module1Class2
Now when I run Module1Class1 from IntelliJ my current working directory is where ProjectParent directory, but when I run Module1Class1 from eclipse, my current working directoru is ProjectParent/Module1 directory.
Why this is different in intellij & how can I change this, so my current directory is always from where my class started execution like in this example 'Module1Class1' directory.
You should edit the template Run/Debug configuration and specify $MODULE_DIR$ variable in the Working directory field.
All the new configurations will inherit this default setting and the variable will be substituted with the directory of the module that you want to run.
There is a feature request to make it the default setting, please vote.
You can edit the working directory from within the run configurations dialog.
We use Eclipse with projects in CVS. It has proven to be the simplest to create a new workspace when having to deal with another branch or application, and then use Team -> Import project set to get all the needed projects from CVS.
Unfortunately, I then have to do the following each and every time:
Change text font to Consolas 11 pt
Disable spell checking in text editors
Run everything in the background
plus some more of the same.
I'd like to change the standard values once and for all in the Eclipse distribution files after having unzipped the distribution (Windows). Where are these defaults located inside Eclipse?
EDIT: For now we just have a preference file which must be read in. An extra step, but works...
EDIT 2014: I've ended up creating a workspace with the settings I want, and then creating a new copy everytime I need a new one. Also handles Maven Central information etc. Accepted the oldest answer saying essentially this.
You can export your settings from a workspace and import them into any other (this basically does what VonC's answer says, but with some measure of error checking).
To do so, in the source workspace select File->Export...->General->Preferences, then select Export All and enter a file to export to, then Finish.
You can then import the preferences into any workspace by doing File->Import...->General->Preferences, browse to the preferences file and hitting Finish.
I have created a clean workspace with all settings i want to have. This workspace i have copied into a save folder i will never delete ;)
When i want to create a workspace for a new project, i copy the confugured workspace and thats it.
I have than configured the svn repository path, code format (you also can import preferences in eclipse for this), view configuration and so on.
Try checking:
<workspace>\.metadata\.plugins\org.eclipse.core.runtime\.settings
Some .prefs files could be interesting to copy from one workspace to the next.
(like org.eclipse.team.cvs.ui.prefs which contains any non-default value for CVS settings)
I want to fix the defaults inside Eclipse so the workspace is created with the defaults I want every time.
Hard core solution:
Change the .options file within <eclipse-SDK-3.5-win32>\eclipse\plugins\org.eclipse.team.cvs.core_3.3.200.I20090430-0408.jar (that is the name I have for eclipse3.5)
That is the file with the default values, at least for CVS.
Here's what I do:
Start up Eclipse
Without importing or starting any projects, edit the workspace and make all the config changes you want
Open up the workspace in windows explorer and create a copy of the folder.
Then, any time you wish to use it, copy this folder to your new workspace folder location - you may need to create the new workspace folder first (and definitely call it something different.)
This is what I do anyway. Yes, it's very dirty but it does get what I want pretty quickly!
When importing preferences (Rich Seller's approach above), especially those that were created by someone else, make sure you backup your Eclipse environment first. That's easy, since it's portable - just copy it to a temporary location.
I've totally hosed my Eclipse environment importing preferences in the past, and importing my own preferences, which I exported just prior to importing, did not fix my issues.
Fix the defaults inside Eclipse so the workspace is created with the defaults, If you want every time suggestions, then
Go to eclipse extract path -eclipse\configuration\.settings -> Edit -org.eclipse.ui.ide ->
Change value to true: SHOW_WORKSPACE_SELECTION_DIALOG=true
Similarly in this file you can directly change other default settings.
I find a way to do that:
open <eclipse>\plugins\org.eclipse.cpp.package.cpp_1.4.2.201210131-1456\plugin_customization.ini
and add
folding
org.eclipse.cdt.ui/editor_folding_enabled=true
org.eclipse.cdt.ui/editor_folding_preprocessor_enabled=true
then new project will enable folding by default.
So
You need to know where your prefer locate, in my case, folding is under org.eclipse.cdt.ui, you can try on an project then check <workspace>\.metadata\.plugins\org.eclipse.core.runtime\.settings to find it
"org.eclipse.cpp.package.cpp_1.4.2.201210131-1456" should be variable depends on what version you use. whatever it's, you should find "plugin_customization.ini "