Can Eclipse have multiple output folders just like multiple source folders - java

I have an Eclipse project, which has two source folders - 'src', and 'test'. I would like the build from both these folders to go into separate output folders.
The reason being, both these folders have a property file called ServiceProviders.properties which has different values for production and test runs of the code.
The problem is, since there is only one output folder where all compiled classes are copied, the properties file in 'test' does not get copied, and as a result my test code gets the production version of the properties file. I guess I am trying to simulate the way we can have different classpaths in Maven (and their lookup order).
If this does not work, I will use a JVM property to specify certain values which I can use in the test case. However, it does not seem like an elegant solution.
Using Maven will get this to work, but I cannot use Maven in this project.
Update 1:
Thanks for both the answers. Since they were identical, I have accepted the one which was posted earlier.
Upadte 2:
Even though I could get multiple output folders for corresponding source folders, it did not solve my original problem which was to load a ServiceProviders.properties from test when I run test cases, and the one from src when I run the app on a server from Eclispe. The way I got it to work is by creating a separate jar file with ServiceProviders.properties file from the test folder. In the run configuration of my test cases, I add this jar file and change the order, so it appears as the first dependency for lookup.

Yes it can, i just have checked that.
If you go into Properties of your Project, and there to Java Build Path, where you choose your different Source-Folders, there also is an option called "Allow output folders for source folders", which, if checked adds an editable output-folder for each of your source-folders.
just select it and click the edit-button and you're fine

Easy: click "Allow output folders for source folders" on the Java Build Path | Source tab. Then, when you expand each folder's settings, you get "Output folder"

In the PackageExplorer view click on the source folder and select "Configure Output Folder..." You can now configure the output folder separately for each source folder.

Related

One existing Java Maven project suddenly not generating .classpath file in Eclipse

I've been using Eclipse 2019-12 since the day it was released. I have a bunch of different related projects that are all SpringBoot Java Maven projects, which were imported from my local git repositories.
In general, all of these work perfectly fine.
Yesterday, I discovered that I couldn't run unit tests in Eclipse for one particular project. This project had been in my workspace for quite a while, and I do things with it almost every day, including running unit tests. What I saw yesterday is that all my attempts failed because it couldn't find the test class file. Other similar projects had no problem with this. I viewed the settings for the project, and there was nothing unexpected. The src/test/java directory was a source directory, and I could use "Open Type" to open the test class. I tried doing "Maven Update". No change. Running "mvn package" from the shell works fine, and I can clearly see that it executes all the unit tests.
I tried deleting the project and reimporting from git. No change.
I then tried deleting the project again, and then manually deleted the .project, .classpath, and .settings files and folder, and then reimporting from git. This made it worse. Initially, it didn't appear to know it was even a Java project.
I then deleted the project again and edited the .project file from another editor. I compared it to to one of the other .project files. It only had the "maven2Builder", and didn't have the "javabuilder" or "springbootbuilder". It also only had the "maven2nature", and didn't have the "javanature" or "groovyNature". I manually copied those elements from the other .project file and reimported the project.
At this point, it knew it was a Java project, but it only added the root "src" directory as the only source folder, instead of src/main/java, src/test/java, and others. I also noted that it still didn't have a .classpath file. So, I deleted the project again and created the .classpath file with an external editor. I copied in the .classpath file from a related project. The structure of the two projects were identical, so this barely required any changes (the other project had one additional source folder that this project didn't have, so I deleted that entry). I then reimported the project again.
The project now "looks" normal in the package explorer. However, I STILL cannot execute unit tests in this class. I have come full circle. I can open the test class with "Open Type", but executing it fails with a ClassNotFoundException in the console.
If I search for the particular test class file in the shell, I find it in the expected locations in both "bin" and "target/test-classes", in the correct package directory.
I looked in the Eclipse error log, and there was nothing significant there.
Curiously, I'm seeing no issue with "Run/Debug As ... Spring Boot App". This finds all of the required class files and starts up fine. I just can't run unit tests.
I don't know what else to try.
Update:
As unit tests in other projects are working fine, I tried comparing aspects of one working unit test with one in the bad project. I first looked at the run configurations, and I stepped through each tab, going back and forth between the two run configurations, and the only difference I saw was the name of the project, so I found nothing of interest there.
I then looked for the working class file in the shell in the working project, and what was curious is that I did NOT find it in the "bin" tree. In fact, the working project didn't even have a "bin" directory. I then verified that the project properties didn't even refer to a "bin" directory, including any of the "output folders", in both projects.
So, I then deleted the project again, and then deleted the "bin" directory from the shell, and reimported the project. Unfortunately, still no change in the overall symptom. It still gets the ClassNotFoundException trying to run the test class.
Update:
I'm running on Windows 10, and I decided to try using SysInternals ProcMon to monitor system calls (like Linux (s/d)trace) while I attempt to run the test. I filtered for the name of the test class. The result showed one possible clue.
This is a somewhat elided image of the list of events that I saw in procmon when I ran the unit test:
I don't know why the operation is "Create File", but I note that the folder path referenced here is "target/classes", not "target/test-classes". Despite the fact that the class file is in the "target/test-classes" tree, I saw no system call that referenced that directory, only "target/classes".
Update:
I tried going through the steps of manually creating a JUnit run configuration. When I first selected the project that is working and then clicking "Search" on the "Test Class" field, it brought up a dialog with many classes in the "Matching Items" list. When I instead selected the project that is having this problem and then clicking the same "Search" button, the list of "Matching Items" in the dialog was empty. I'm not sure what to do with that fact.
Update:
I decided to try adding more verbosity when it tries to run the unit test class, both in the working project and the NOT working project. In both run configurations, I added "Xdiag -XshowSettings".
When I compared the results, I found that both showed a "java.class.path" value that included all the expected directories, being "target\test-classes" and "target\classes", even the one that is NOT working. However, note the very subtle difference:
Working:
java.class.path = C:\Users\<myuid>\git\futurebillestimatorms\target\test-classes
C:\Users\<myuid>\git\futurebillestimatorms\target\classes
NOT working:
java.class.path = "C:\Users\<myuid>\git\checkoutms\target\test-classes
C:\Users\<myuid>\git\checkoutms\target\classes
Note the one character difference, the double quote at the beginning of the value in the NOT working sample. There is a matching double quote at the end of the long list of classpath entries. The one that is working does not have those double quotes. The double quotes around the java.class.path value are the only double quotes anywhere in the output.
Update:
Ok. I have a workaround. This is an issue in Windows with the 2019-12 release. This is the bug report that describes the problem: https://bugs.eclipse.org/bugs/show_bug.cgi?id=558495 .
The simplest hack for me at this point is to check the "Use temporary jar" checkbox in the Run Configuration.

Duplicate source folder when creating a maven web project

When I am trying to create a new web application project using maven, I am getting duplicate src folder in the project directory structure. Is it expected or I am doing anything wrong?
If it is expected why is it creating two directories with the same name, Is there any specific reason?
It's not creating two directories with the same name. Just check with your explorer, or the command line.
It's just Eclipse showing two different views (one as a directory of the project, one as a Java resource directory) of the same directory.
They both are same. Only thing is your IDE is showing first one in Java build path format and other one as normal. If you go to folder structure you would notice that they are physically same path.
This happens when the folder structure in the build path.
But still since your IDE is differentiating them, use the first one in the picture -> src/main/resources

Idea and eclipse return different result for same piece of code

In my project, I am extracting the test data to some location which needs to be used by junit tests. When I run my test from eclipse they run fine but when I run them from idea they fail because they could not locate the data.
I am extracting data at in my projects root Folder.
I am using following code for locating the data on disk
TestData.class.getProtectionDomain().getCodeSource().getLocation().getFile()
In eclipse it returns
F:\dev\shopping\testData
but in Idea it returns
F:\dev\out\test\testData
Can someone explain this behavior?
IntelliJ puts all binary files in the out/ folder per default (which also means that that will be the working directory).
You can change that folder for your entire project or for certain modules by going to File -> Project Structure...
More information here: https://www.jetbrains.com/idea/help/configuring-module-compiler-output.html
You can just change this folder to be the same as in Eclipse or provide some other method of finding your test data.
Besides that, you could also just change the working directory of your run configuration to match the one Eclipse is using as well. More information on that here: https://www.jetbrains.com/idea/help/run-debug-configuration-application.html

Use a Java file that is in a different place than the rest of the project

I have a java source file in my project, that I want to move to an entirely different location than the rest of the files (my unix home dir), but I still want to be able to properly use it in my project. Is there a way to do this?
EDIT - I see there is a vote to close this for being unclear, so - let's say you have a pJava project in Eclispe. All the source files are neatly saved in their relevant packages, under the same directory. Now, I want to move one of the files to somewhere completely different, but still have it used in my project. I hope this clarifies
Thanks!
I suggest that it depends on what your reason is for moving the source file.
The Java file is still conceptually part of this project, but you're moving it for general organisational purposes. In which case, the new "completely different" directory is another place where sources should be read from, which most IDEs will call a "source root". You should configure your project to read sources from there as appropriate for your IDE.
You're moving the file because it's distinct from this project. In which case I would suggest it ought to be a separate project in its own right. In order to still use the logic in your original project, you'd build the new project into a JAR, and then bring in the JAR as a library dependency (either directly, or using some dependency management system such as Ivy/Maven/etc.). Again, the details will depend on what your current setup is.
Right click on your Eclipse's project -> properties -> java build path , and under the source tab click Link source then choose the parent folder of your java file .

Eclipse - Source not found

I know this question has been asked many times before, but none of the proposed solutions resolve my issue (or I'm not implementing them correctly).
I'm developing a plugin for OpenFire, and when I set a breakpoint in my plugin source Eclipse reports "Source not found". The JAR is built separate from the build of the OpenFire server. I've tried adding the JAR and specifying the source code directory, but no dice.
Here's the process I'm following: When I hit my breakpoint I'm clicking "Edit Source Lookup Path", clicking "Add", Java Library, User Library, click User Libraries, adding my library (tried both "Add JARs" and "Add External JARs"), and then specifying the folder that contains the source code. I've also tried:
Adding the source by selecting "File System Directory" instead of "Java Library"
Adding it by selecting "Workspace Folder"
In the Project Explorer, adding the source for my plugin to the Java Build Path
In the Project Explorer, adding my JAR as a Library and specifying the Source attachment
Thanks in advance for any suggestions.
Personally, I have no good experience in Eclipse JDT, when adding a source folder at the time it hits a break point. What I always prefer to do, is adding the source folder before debugging:
Select Project/Properties/Libraries
Select your library and "edit..." the source attachment
Select either the corresponding source folder or the zip/jar containing the package
After that, you should be able to open the source files in the virtual "Referenced Libraries" folder (directly below the "JRE System Library" folder inside the project), or when opening a reference to a linked class file from within the JDT editor. If you are able to access the source, then you should also be able to at least stop at the break point and see the corresponding source.
Else, you will have to check again, if the source folder is really valid: The source folder or source archive must contain the folder with the name of the root package of the library (e.g. the default Java "src.zip" inside the JDK folder also includes a corresponding "java" folder at its root). When in doubt, extract the archive and select the parent folder of the package you are interested in - sometimes source archives might be a mess or incompatible to JDT.
If this has been assured and it still doesn't work, chances are, that your linked source folder does not correspond to the compiled version of the library. Usually JDT will handle such inconsistencies fine, but if you try to open a source file, that is entirely different than the corresponding class file, you will encounter problems. In this case I would suggest either downloading the correct source version of the library, or recompiling the library from the source, if all else fails.
If opening a referenced source file from withing the JDT editor does work fine, and you are still unable to open the source files when a break point is entered during debugging, then most likely the class files are missing the line numbers of the corresponding source file. Again, you will have to recompile the library from the source in this case.
Finally, it is also possible to overwrite the default source lookup by specifying source folders or archives in the source tab of the launch configuration. But you should normally not need to do this, when your build path is configured correctly. From the Eclipse Help:
The Source tab defines the location of source files used to display source when debugging a Java application. By default, these settings are derived from the associated project's build path. You may override these settings here.
I had this very annoying problem for a long time but was finally able to solve it. In my case, a null pointer exception was being thrown somewhere in Java's Transformer.IsRuntimeCode(ProtectionDomain) function.
I didn't really need to know about this since the exception was being caught and handled, but eclipse would pause debugging every time this happened and tell me that the source wasn't available. As a result, I constantly had to keep pressing the button to continue code execution.
In order to prevent this from happening, I:
1. Clicked on the "Breakpoints" window at the bottom of the debugging
screen
2. Right clicked "NullPointerException"
3. Unchecked "Caught"
This prevented the debugger from pausing program flow during a caught NullPointerException.alt text
(source: SharpDetail.com)
And this another one as:
Eclipse doesn't crash. You're trying to step into a method, where eclipse doesn't know where to find the source (*.java) files and informs you about this. Here is how to tell eclipse where to look. Go to
Window->Preferences->Java->Installed JREs,select the JRE you are using and click Edit.
There, select all of the jar files in the list you see and and click Source Attachment....
In the window that shows up, select the fille src.zip, which is in your JDK folder (if you didn't uncheck it while installing the JDK). On the machine I'm on right now, that is
C:\Program Files\Java\jdk1.7.0_07\src.zip.
Save all your changes (possibly restart eclipse) and you won't see that error again.
Well it turns out the solution was anticlimactic. When Balder's recommendations did not work I tried debugging one of the stock OpenFire plugins and it worked just fine. I then created a new plugin from scratch, and by doing nothing other than adding the source to the project (Right-click on the project -> New -> Source Folder) it also worked just fine. I have no idea why Eclipse refuses to see the source for my original plugin, but I moved all my code and libraries over to the new plugin and debugging is working as expected.
Many times you put jar files in eclipse IDE as referenced libraries when binary version of application was downloaded. Usually done so by configuring the build path. But the binary does not have the source files. You have Binary version and source version of application.
One simple way is to download the source libraries that you also use maven to build the project.
Keep somehwere, may be inside your project workspace.
Now while seeing the class file (from eclipse, jar exploded) you may see the source not found, fine.... there is a button below and click on that, a new window opens and there select add external folder.
Reference it to the src folder of the source you downloaded(not the binary one) and kept somewhere as said above and it will show the class details from that.
I fixed this issue with doing the following:
Click at the menu Window - Preferences - Debug - Step Filtering And check all the packages like the following image.
(Step Filtering)
Then, debug again your project and thats it.
Best regards,
In my case, I had a breakpoint in the class declaration. I mean in the next line.
public class GenerateInterface implements JavaCall {
So, the debugger stopped in that line and showed the following message:
Source not found
I think unconsciously activate the breakpoint in that line.

Categories