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

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.

Related

How can I clean up my eclipse project after deleting a .java whose .class file still seems to be somewhere?

So, I deleted a couple of .java files from within eclipse because I didn't need them anymore. However for some reason another class still uses them and compiles without a problem. When I go to the declaration of the constructor of one of the deleted classes a .class file opens of that class even though I thought they got deleted by eclipse when I deleted their .java files.
I have tried to clean the project and I tried to find the .class file using the system file explorer in the folders that are listed in
project > properties > resource > linked resources. However I couldn't find them.
Now, since I deleted those files/classes there should be a compile time error wherever they are used but everything compiles without an error, even using the code that should've gotten deleted. A colleague that recently joined the project does receive compile time errors, since he doesn't have those class files. And I want to get them too, so I can fix the code where the classes were used. Of course he could show me the lines of code that are affected but if this happens again he would probably be affected as well as he would have the class files by then.
Try doing the following
Go to project > properties > Java Build path >Source
under that you can find Default Output Folder section, simply delete the classes folder under that location with in your project.
Try cleaning the project.
Or
Simply close the eclipse and reopen it again, sometime this would work for me.
Hope it resolves your problem
If you just deleted the sources, they binaries would still be there. So open the Navigator View and go to the project in question. Look for the bin directory and delete the appropriate class files. Otherwise, delete the project itself.
You should also ensure that no other programs depend on those class files.
I found the problem. Someone built a .jar file I didn't know of which contained a lot of old code.
I found it by right clicking the constructor and selecting
references>project

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

Runnable JAR not working with referenced libraries

I want to create a runnable JAR that include referenced libraries, namely jackson, in eclipse via the export. There used to be fat jar that seemed to be the goto solution before Eclipse offered to export to runnable JAR.
From this question, the first option would be what I'm looking for. Since it is for the moment a small project, it wouldn't be a problem to always have everything together (plus it's easier for me since I'm just starting with JARs, runnables and jnlp)
When I try to launch it, there's an alert window saying there was a problem and to try to find it in the console (which I can't get to open).
If I remove any use of this referenced library everything runs perfectly (except the part that it's not doing what I want at all). The window looks the way I designed it, but no functionality without the library.
I don't even have a stack trace to help with the problem. I've created other projects without any referenced libraries and everything is fine. So I've pretty much narrowed it down.
Since I let Eclipse handle the exporting and everything, I don't suppose you need the manifest to see if it is alright. Any questions you might have, I'll provide the informations you need. It goes without saying that the project runs directly from Eclipse.
Eclipse
Version: Indigo Service Release 2
Build id: 20120216-1857
Edit following Baqueta's answer
What happens when I do that is that I get a custom RuntimeException. I'm a bit baffled here. Here's a simple explanation of my code.
I have a utility class (UtilityJSON) that uses the referenced library (jackson). The constructor of that utility class instantiate an object defined in the referenced library, let's call it mapper.
A method of UtilityJSON uses a method of 'mapper'. At this point the exception is raised. Why I'm baffled is that 'mapper' is succesfully instantiated but using its method raises an exception. If the jar couldn't find the referenced library, it probably couldn't create the object 'mapper'. But running the project from Eclipse is all fine.
Is there any way to debug when running an executable jar? Maybe echo something in the console.
Edit 2
I may have found the error. My program reads a *.txt file in a folder inside the project. When exploring the content of the jar I see that this folder was not included, thus the exception. So the question now becomes: how do I include this folder? It's in the root folder of the project.
You could try the following:
In Eclipse, go to Project->Properties.
Select 'Java Build Path' from the column on the left.
Select the 'Order and Export' tab.
Find 'jackson' in the list and make sure it's checked.
EDIT
To answer your updated question:
In Eclipse, find the *.txt file in the Package Explorer. Right-click it and select Build Path->Add to Build Path. Then follow the instructions above to make sure the file gets included in the Jar. Finally, do a clean and rebuild.
If you're ever going to add more resources, it is common practice (and makes sense!) to have a resources folder (often called 'res'). You can then add the entire folder to the build path, so that all the resources in there get included in the Jar.
Enable the java console by writing
javaws -viewer
in a terminal. Close the Cache Viewer and click the advanced tab. There open Java-Console and click "enable console".

Can Eclipse have multiple output folders just like multiple source folders

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.

Eclipse run old version of program

I have a problem while runing my application in eclipse. I make some edits like this:
String res = "newvalue";
System.out.println(res);
But when I run the application I still receive "oldvalue" in output. After this I tried to delete main() function - and I run application and I see "oldvalue" in output again.
Maybe it's some cache in JVM, or smth. else?
UPD:
It's 15:35 on my clock now. But in /bin folder I see .class files with 14:33 timestamp. I change source files (.java), press ctrl+f11 in eclipse and files in /bin folder are still 14:33 ...
UPD2:
After cleaning the project I receive the following problem:
The project was not built due to "Could not write file: D:\projects\NelderMead\bin\ru.". Fix the problem, then try refreshing this project and building it since it may be inconsistent
SOLUTION
The problem was that eclipse can't write file to the folder with spaces and UTF chars in it's name. So, I copy project to the new clean workspace and it runs without problems! Thx all for help detecting the problem!
You're executing an older class files, the reason could be
a compile error somewhere else (see problems view)
or your changed accidentally the source path so that the new source no longer gets compiled.
Try to clean the project and make sure the new classes are compiled to your output folder.
The JVM doesn't have a cache for class files.
First, make sure the file is saved (there is no asterisk next to the file name in the tab). Usually files are saved automatically, but you could choose not to save files and never ask by mistake, the first time Eclipse pops up this dialog.
Then see what's going on in the Problems view. You might see something like project cannot be built due to the following reason. It may be because of compilation errors in the dependent projects.

Categories