Issue
By "controls" I suppose I mean a couple of things.
Basically I struggled a bit early on in the java application for my Case Study working with Eclipse to configure everything properly get it to be error free and then run, and finally I got it working through some POM settings and maven>updating project and didn't have any more issues. Then I downloaded our whole cohort's case studies and I got a lot of similar errors (red Xs in Eclipse) in most of the projects, and it seems like if I go through them one by one and configure version, among other things, they work.
So my understanding of Java/Eclipse errors and their relation to Maven and JRE version is kind of fuzzy but this is my working model (which may be wrong in some parts):
Each version of Java adds new features (obviously), and Java 1.8 was a major milestone but for the most part, for most of these projects, it seems you can pick and choose whatever version you like, as long as you configure your project settings properly. It's dealer's choice basically (?). If you compile in the command line, these things may not apply or may be passed as args.
But to configure your project properly, I guess you can go into build path and do it that way (?), but the preferred way is to have maven handle the dependencies, including the core Java Runtime library dependency (the language version itself) and you do that by setting a property in the pom.xml (which may or may not be present. Which is optional but I think recommended). Then (when you maven>"update project"), it syncs and overwrites what is the default, and it it is the system or IDE default that is causing the intial conflict. (?). Do I understand this correctly?
And maven/Eclipse knows to respect maven more than the build path or it replaces the build path, because it understands that it wants an all-in-one source of configuration truth?
And having the wrong version of java specified, even if it's closely related versions like 11 and 13, can make it seem like you have tons of errors in your code. It can't even find the common classes like String, because they are technically different, because that's just the way Java configured it, unlike perhaps other languages.
When you import a project into your IDE (e.g. Eclipse), you are given by Eclipse a default JRE version based on your IDE settings or what you downloaded, or something, but the project may expect a different version (why or wherefore, I am not sure but this seems to be the case and what throws errors or makes Eclipse throw errors). Yet if I update the maven project by right clicking, it doesn't necessarily fix the issues. My understanding is because the pom file is not complete enough, with missing properties that should ideally be there, for this very reason: e.g. source, target and release
I had a project that threw erorrs that seemed to go away when I changed
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>15</release>
</configuration>
</plugin>
To
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<release>11</release>
</configuration>
</plugin>
Adding the source and target and changing release to 11.
It might have worked at 15 but I don't remember downloading 15 ever, and I'm not sure it had to be on my system or if maven downloads it.
Maybe Eclipse was screaming that the source did not match the release, because I didn't have 15 on my system. My case study had 11, so that's why I chose that even though I think I have 14 on my system. There's too many moving pieces, but that's what I want to figure out once and for all- what is going on.
Target and release seem like the same concept, so maybe they are duplicates. Maybe they are instructing the compiler or IDE to the same end. It doesn't seem like the source should have to match the target or they wouldn't give us the option.
I'm trying to figure why I had errors.
What gets me is it seems that the imported projects know enough to know the Eclipse Java version is wrong, but it doesn't know enough to update to the correct when I updated the maven projects. We were employee-students in a nice long paid training so we couldn't be expected to know everything but would this be due to the fact that the pom.xml config files that are part of the projects are sub-optimal and incomplete in themselves that require additional config that I had to do, or are they fine and the problem was on my end? What seems funny is the system seems to know enough to know things are wrong (wrong version) but not enough to know how to make it right (or at least not given the command).
But if I choose a random suitable version (11) and I put that in a few properties in pom, (target, release) and update, then it seems to work. It overwrites the Eclipse default, the prior pom project default, if any, wherever that is coming from or being inferred from, and my new (pseudo random) version becomes the source of truth and it is happy, not that it needed a particular version, but that it just needed someone to state a fixed version plainly. In that sense, nothing 'controls' which version has to be used. You just have to assert it in the right place, as long as your basic code features are supported (and they probably are).
I don't know if I understand this right. There are many moving parts that I'm always wrestling with. I thought this would be a good learning opportunity to figure this aspect of the language out or make progress. I think Eclipse and maven are doing a lot of things implicitly that I'm not aware of, that I need to be, or there are settings I don't fully understand that are being read, and you can't know what needs to be just by using logic, because of implicitness, and also Java is funny and more strict with its language versions, in that a String in v 11 is not the same class as a String in v 12, if I understand right.
Knowing how my mind works, liking to deconstruct the moving pieces and see how it works and fits under the hood mechanically, if anyone has any good reading material on the topic, I'd appreciate that too. I love coding. I don't necessarily love configuring, especially when I don't know what I'm configuring and why.
Solution
Note the following:
- There is a difference between the installed JDK and the target Java version. The installed JDK needs be at least as high as the target Java version.
- The only relevant Java versions at the moment are 8, 11 and 16. All others are obsolete and should not be used.
- Fundamental classes as "String" do not change from one version to the other, but Eclipse tends to show strange errors.
- The truth is always on the command line. If you want to find the real errors, run
mvn clean verify
. Errors in Eclipse may be misleading or just rubbish.
Answered By - J Fabian Meier
Answer Checked By - Willingham (JavaFixing Volunteer)