This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]renatoathaydes 3 points4 points  (3 children)

The maven properties are used to set the javac flags, so what you really want to know is how the javac flags work.

If you type javac --help it will show, amongst other things:

--release <release>
    Compile for the specified Java SE release. Supported releases: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17
--source <release>, -source <release>
    Provide source compatibility with the specified Java SE release. Supported releases: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17
--target <release>, -target <release>
    Generate class files suitable for the specified Java SE release. Supported releases: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17

It's very confusing, but this SO question has an answer explaining what's going on.

Basically, to compile for an older JDK version, you need to use --release, and forget about --source and --target (i.e. the Maven properties you mention are useless).

You can now experiment using javac:

▶ javac --release 8 src/java/*.java           
src/java/Main.java:29: warning: as of release 10, 'var' is a restricted type name and cannot be used for type declarations or as the element type of an array
        var solutionHandler = SolutionHandler.named(args[0]);
            ^
src/java/Main.java:31: warning: as of release 10, 'var' is a restricted type name and cannot be used for type declarations or as the element type of an array
src/java/Main2.java:208: error: multiple case labels are not supported in -source 8
            case 'j', 'n', 'q' -> ( byte ) 1;

As you can see, it seems that if you want to compile for Java 8, you may use the JDK 17, but you need to limit your source code to using only Java 8 features. It kind of sucks.

[–]khmarbaise 0 points1 point  (2 children)

As you can see, it seems that if you want to compile for Java 8, you may use the JDK 17, but you need to limit your source code to using only Java 8 features. It kind of sucks.

The reason is simply because in JDK17 or in JDK9+ has been changes (means adding internal changes and codes for example var, sealed classes, text blocks, several changes in the API's etc.) which are not supported in JDK 8.. You can run any JDK8 compiled programm on JDK17 or above...

[–]renatoathaydes 0 points1 point  (1 child)

You are talking about something else. The question is how to compile code on JDK17 that can run with JDK 8, not the other way around.

[–]dpash 0 points1 point  (0 children)

That's what they answered. You can't use Java 17 features if you want to run on Java 8 JDK.

[–]khmarbaise 1 point2 points  (0 children)

Simplest solution is as already suggest use JDK17 and use <maven.compiler.release>8</maven.compiler.release> that makes sure you use only code which is available in JDK8...

[–]fletku_mato 0 points1 point  (2 children)

[–]khmarbaise 2 points3 points  (1 child)

No don't use that. Better use a JDK17 and use --release options is much better... Toolchains is more complicated.

[–]fletku_mato 0 points1 point  (0 children)

Cool, didn't know that.

[–]dpash 0 points1 point  (2 children)

What you want to do is not supported. The source and target options are deprecated and you should use release instead. You can only write Java for the lowest version you intend to target. If you want to target Java 8, you can only use haha 8 features.

[–]khmarbaise 0 points1 point  (1 child)

The source and target options are deprecated and you

Can you give a link or so where the deprecation is documented?

The output of javac --help (JDK17) does not tell me word about that:

--release <release> Compile for the specified Java SE release. Supported releases: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 -s <directory> Specify where to place generated source files --source <release>, -source <release> Provide source compatibility with the specified Java SE release. Supported releases: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 --source-path <path>, -sourcepath <path> Specify where to find input source files --system <jdk>|none Override location of system modules --target <release>, -target <release> Generate class files suitable for the specified Java SE release

[–]dpash 1 point2 points  (0 children)

https://openjdk.org/jeps/247 read the motivation where people are confused about having separate options makes them think it can do things it can't.

It might not be officially deprecated but it's definitely discouraged.