1

I have this code and I want to know which Java versions can run my code without having to rewrite it. Is there a way to know this using eclipse without installing any other tools or plugins?

Seems like elements like LocalDateTime have this in their java documents "Since:1.8" which means(correct me if I'm wrong) that if this syntax is used in java versions lesser than 1.8 then the code will have to be changed at some point

mylkcha
  • 89
  • 9
  • Seems like you are you asking for minimum version required - yes if you use syntax or APIs that have been introduced at a certain version, you'll have to change it if you want it to run on an earlier version. – Hulk Oct 04 '18 at 10:15
  • *Since:1.8* means your code won't compile if you are running it on an earlier version. – achAmháin Oct 04 '18 at 10:15
  • The notation "Since: 1.8" doesn't get in the Javadoc by any magic. The programmer who wrote the class had to put ` @since 1.8` in the Javadoc comment. – Kevin Anderson Oct 04 '18 at 10:18
  • Possible duplicate of [How to determine the Java byte code version of the current class programatically?](https://stackoverflow.com/questions/1707139/how-to-determine-the-java-byte-code-version-of-the-current-class-programatically) – Kirill Oct 04 '18 at 10:27
  • @g4s8 its not, the one you linked was marked also as duplicate of another. which eventually asks for a programatical way to do it. Here he is asking of a more hands on way but not programatically. – Sir. Hedgehog Oct 04 '18 at 10:31
  • @hedgehog this answer may help: https://stackoverflow.com/a/7970837/1723695 – Kirill Oct 04 '18 at 10:33
  • 1
    Are you asking how to determine whether *already-compiled* classes can be used, or if *source code* can be compiled and used on a particular version of Java? – Andy Turner Oct 04 '18 at 10:34
  • @AndyTurner a little of both actually. Since there's already a jar file, the code can already be used for java versions (*insert java versions*). But at the same time, it is also expected that the source code will be edited(thus the reason why there are also .java files). Therefore keeping these two in mind, I'm wondering how to know the mininum java version required to be able to do both? – mylkcha Oct 04 '18 at 10:46

2 Answers2

3

In Eclipse under Properties you can change the compiler compliance level for the project to different versions.
If parts of the code are not available for the selected compiler version it will mark those parts and show the required version. enter image description here

Turamarth
  • 2,282
  • 4
  • 25
  • 31
2

Well, you don't need tools or plugins for that. The only thing you need to know is in which Java version you are developing. E.g if you are using Java 8 you can't expect a system running Java 7 to run it.

But if you are using Java 7 for example it will run on a Java 8 machine.

This is not applicable to all Java versions. For every version compatibility you need to refer to the Oracle guides.

Java 7 - 8 Compatibility

Also, If you dont know the exact version of a code that was given to you, then you should know that every Java class has a version stamp:

Two unsigned short integers starting at byte offset 4, right after the 0xCAFEBABE magic number.

They are the major/minor version numbers of the class format (see the Class File Format Specification), and they have utility besides just being extension points for this format definition. Every version of the Java platform specifies a range of supported versions.

Hulk
  • 6,399
  • 1
  • 30
  • 52
Sir. Hedgehog
  • 1,260
  • 3
  • 17
  • 40
  • Thank you for the answer, but what if I don't know which version the code was developed? Is there a way to know this? Especially in cases wherein I just received a jar file along with its java source codes. – mylkcha Oct 04 '18 at 10:20
  • 3
    @mylkcha you can search for simple signs of Java versions, e.g. "<>" likely indicates Java 7+; "->" indicates Java 8+; "var" indicates Java 10+. The absence of such signs will tell you nothing; but the presence puts a lower bound on the version number. – Andy Turner Oct 04 '18 at 10:24
  • @mylkcha Every Java class has a version stamp You may not be aware of it, but every .class file you generate contains a version stamp: two unsigned short integers starting at byte offset 4, right after the 0xCAFEBABE magic number. They are the major/minor version numbers of the class format (see the Class File Format Specification), and they have utility besides just being extension points for this format definition. Every version of the Java platform specifies a range of supported versions. – Sir. Hedgehog Oct 04 '18 at 10:25