IntelliJ Problem with+get current working directory - java

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.

Related

Confusion over current working directory in File’s getAbsolutePath method

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.

Default working directory for JUnit test - how can we define it consistently in Eclipse?

According to documentation,
By default the classes in the java.io package always resolve relative
pathnames against the current user directory. This directory is named
by the system property user.dir, and is typically the directory in
which the Java virtual machine was invoked.
So, according to it, if I run a JUnit class from the popup menu, choosing JUnit test, the current folder should be the folder in which the test class lies.
But really it takes the Default Working Directory from the page of Run Configurations - Environment page for the last Run Configuration used. So, if the test uses files with relative paths, mostly it cannot be launched correctly from the popup menu.
Even if I create a Run Configuration for the JUnit test class, yes, it will automatically set its Default Working Directory (On Run Configurations - Environment page) to the folder where the test file is. But still, used through popup menu, it again takes the Default Working Directory from the last Run Configuration used.
How can I set default directory or Eclipse somehow so, that I will can launch my test classes with the current folder according to documentation?
You didn't provide an example of what you try to do but I guess you try to load some file which you need in your test. If thats the case you should better change the way you load the file.
YourTestClass.class.getResource("filename") orYourTestClass.class.getResourceAsStream("filename") is what you should use. It looks up a file relative to YourTestClass.

Eclipse IDE : How to know exact jar file for classes loaded at runtime?

I have web application that is running in Jboss container.
Sometime I need to debug the code and for the I need to know from which jar current class is being referred (so that i could search of source jar of the corresponding jar)
Is it possible to get this Jar information (name of the jar file) from which current class is being referred. See the attached screen shot - from where i am trying to get this jar file information. Not sure if my approach is right or wrong
Eclipse allows you to make code changes during debug time.
So, if you want to entering in a class and know what is the jar source of this class just do CTRL + Mouse Right click in the class name and you enter inside the class.
If you have the "Link with Editor" option selected in your Package Explorer you can see automaticly what is ths jar file you are importing.
Compile time classpath settings are different from Runtime server classpath settings.
So if you want to know all the locations/jars that contain a particular class at compile time in eclipse then use Ctrl+T and type the class name. If there are multiple jar files with same class/namespace, then the order will be defined in Build Path settings --> Order and Export.
Runtime reference depends on class loading policy of your server configuration. But usually first it refers from local project, then manifest and then from common class path library.
If your server is configured to refer the workspace location of your project, then most probably the order would be as you defined in .classpath file, and you can use Ctrl+T to find it out.
Edit
It seems you want eclipse to resolve the source file automatically. If you are using maven then run below commands to refresh the source and javadoc for all your jar files.
mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc

handling main and test without "The declared package com.pkgY does not match the expected package dirX.src.main.java.com.pkgY" in Eclipse

Every Java project I download has main and test folders. Both contain java and resources sub-folders. Some projects even add more levels: for instance dir1, dir2, dir3 and etc in the root, where each dirX contains src with main and test subfolders. How do I handle them? If I just drop the content of the project root into the default src, Eclipse will attribute classes into dirX.src.main.java.com.pkgY obviously resulting in "The declared package com.pkgY does not match the expected package dirX.src.main.java.com.pkgY" error. Am I supposed to add every dirX/src/java directory as a source separately in buildpath, as this answer suggests? What about the corresponding tests? Thanks.
This structure is Maven's Standard Directory Layout. In your case, you need to add both src/main/java and src/test/java (right click on java folder and go for use this as source folder) and resources as well to classpath through configure buildpath option. Refer this maven link for more details on this.
Make sure you installed m2e (http://download.eclipse.org/technology/m2e/releases), and if you're doing JavaEE stuff with Eclipse WTP, you should also probably add m2eclipse-wtp (https://github.com/sonatype/m2eclipse-wtp/wiki)
Then, you have 2 solutions :
either right click on each project and Configure > Convert to Maven project
remove the projects from your workspace and do an "Import as existing maven project"
m2e will take care of configuring Eclipse based on your pom.xml configuration.

How do I save Eclipse launch profiles across workspaces?

When I copy an Eclipse project directory, it contains the .classpath and .project files so that when I take that same directory to another Eclipse instance, I don't have to setup my build path and such (assuming that all the resources are contained in the project, and not external.)
However, this procedure doesn't cause launch profiles to "travel" with the directory.
Is there some other file/directory structure I can "carry around" to another instance of Eclipse that will include my launch profiles?
The .launch xml files (launcher definition) are found in
[eclipse-workspace]\.metadata\.plugins\org.eclipse.debug.core\.launches
Just copy them into your <project>/.settings directory.
Refresh your project, and there you have it: those launcher configuration are available, and you can copy them along the rest of your project files.
Warning: do uncheck the option "Delete configurations when associated resource is deleted" in the Run/Launching/Launch Configuration preference panel: it is common ot soft-delete a project in order to import it back again, to force a reinitialization of the eclipse metadata,... but this option, if checked, will removed your detailed launch parameters!
See also Which eclipse files belong under Version Control, and where are the external tools launch configurations in Eclipse.
Jared adds:
There is no .settings directory in my project directory - I created it and put the .launch file in there, refreshed my project, and it doesn't have any Run profiles. Do you have to mirror the directory structure under .metadata?
Just create the .settings directory (it is actually created when you have project specific settings as illustrated in the question "Maintaining a common set of Eclipse preferences").
Then delete the same .launch file from the .metadata directory.
Then refresh your project and go to the "Run Configurations" Panel: you will see your launch configurations still available, except they are stored where they belong: within your project directory and not buried under an obscure workspace metadata thingy.
Update June 2012
I always seen my launch configurations, saved under PROJECT_LOC/.settings, automatically imported when reloading the Eclipse workspace.
However, as described in "Tip: Creating and Sharing Launch Configurations (2007)", you should be able to reference a launch configuration saved into any subdirectory of your project if you go to the "Common" tab of your "Run Configurations" wizard.
So saving your launcher anywhere in the project won't display them automatically in the "Run Configurations" section of the preferences.
But declaring a new one, sharing its launcher as a "Shared file" (as illustrated above), and then overwriting that launcher with the actual one you had previously saved would update that run configuration.
Try choosing a shared file location inside your workspace from the "Common" tab of the launch configurations (profiles).
Depending on the amount of moving around you do and the number locations you move to, storing your launch files in an online storage space and then setting up folders at the new locations to synchronize with it may be a viable solution.
Dropbox
is a decent online storage system that gives users up to 2 gigabytes of storage.
Here is an example of how you can synchronize folders out side the Dropbox folder curtiousy of
LifeHacker.com :
Use either the JUNCTION utility from Sysinternals, or the MKLINK command built in to Windows Vista and Server 2008, for example:
junction "C:\Documents and Settings\User\My Documents\My Dropbox\DesiredFolder" "C:\Path\To\DesiredFolder"
mklink /D "C:\Users\Steve\Documents\Dropbox\DesiredFolder" "C:\Path\To\DesiredFolder"
Or, if you prefer a GUI, install Link Shell Extension.
[eclipse-workspace].metadata.plugins\org.eclipse.debug.core.launches
would be the DesiredFolder.
You could also use SyncToy to echo changes from another folder to your Dropbox folder. This keeps 2 copies on disk though.
On OS X or Linux, try the following:
Use the ln command, for example:
ln -s /path/to/desired-folder ~/Dropbox/desired-folder
This works with files too:
ln -s /path/to/desired-file ~/Dropbox/desired-file
Another easy way to do this with Terminal is type the ln -s part, then from Finder drag the folder/file that you want into the Terminal window then drag the Dropbox folder and hit return.
Note that an Alias file or folder does not work.
Another solution could be to keep your launch files in an svn repository. Search for How to run Eclipse launch configurations programmatically? on this site. Sorry can only post one url so I used it on Dropbox.
There is an another clean way to share the launch configurations in Eclipse.
Create a java project. For instance lets name it Launches-Configs.
Now create a new Run Configuration for a project that you need to
run.
On the Run Configurations window navigate to Common tab. Ensure
Shared File option is selected and input the Shared File location is set to
\Launches-Configs\src
Check in Launches-Configs to a VCS and share with other teams
I have had good use of creating a /launch folder, and then setting the launch configuration to be "Shared file:" (the Commons panel) to the above mentioned launch folder.
The resulting launch file can be checked into e.g. CVS git and be pulled into a fresh workspace.
Create a Launch Configuration with Shared File option selected under Common Tab.
This will create a configuration file with .launch extension.
Now in another workspace just go to Import and select Launch Configurations, choose the configurations from the directory and finish.
It will appear in the Run configuration now.

Categories