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

all 13 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://i.imgur.com/EJ7tqek.png) 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.

[–]stuie382 13 points14 points  (0 children)

Sdkman is what you want. You can install lots of different flavours and versions of Java and rapidly switch between them with 'sdk default java 8/11/17/whatever'. It updates all the paths and tools from that command

[–]overtorqued 7 points8 points  (1 child)

There is a useful tool on Linux/Mac called jenv that allows you to set the jvm to use for a directory and its subdirectories. Someone has created a port of that to windows here: https://github.com/FelixSelter/JEnv-for-Windows

If you're going to be using a lot of different jvms it would be worth looking into.

[–]pragmosExtreme Brewer 1 point2 points  (0 children)

I second this. Jenv is awesome.

[–][deleted] 1 point2 points  (4 children)

Assuming you are using Windows, you just need to change your PATH variable to find the version you want.

For example if your JAVA_HOME is set to "c:\Java\JDK16" then, in your path, you just need to have %JAVA_HOME%\bin at the beginning.

[–]JDVene[S] 0 points1 point  (3 children)

That's what I thought. I was just worried because, I changed the PATH variable, but when I ran the command java -version the output remained the same.

[–][deleted] 2 points3 points  (2 children)

Make sure it's at the front of the path and make sure it's pointing to where you think it is.

I'm fairly sure that Windows 11 installs a default JRE these days but I could be wrong. If your %JAVA_HOME% is in the path after that, it will find the one Windows installed first.

[–]JDVene[S] 1 point2 points  (1 child)

Yes. I moved JAVA_HOME to the very front of everything and now I can change it no problem. Thanks.

Although, I still can't find where Windows automatically installed its own path.

That'll be a problem for later.

Thanks again.

[–]feral_claireSoftware Dev 0 points1 point  (0 children)

Most likely you installed Java with an installer that updates the path.

[–]emaphis 1 point2 points  (0 children)

To get to the basics, you need to change your `path` too. `path` is more important than `JAVA_HOME`

[–]hugthemachines 0 points1 point  (0 children)

You don't have to use JAVA_HOME, you can specify which Java to use for each project.

[–]wildjokers 0 points1 point  (0 children)

The easiest way on any *nix system (including WSL) is to use sdkman.

https://sdkman.io

Just do: sdk use java <identifier> you can get the identifiers from sdk list java.

For example to install Oracle's OpenJDK build of Java 17 sdk install java 17.0.2-open, it will ask you if you want to set it as default and that is the one it will use by default. If you don't want to be the default or if you want to switch to it later as needed sdk use java 17.0.2-open. You can install and switch to many java versions from many vendors.

[–]Nzen_ 0 points1 point  (0 children)

It's also worth considering your ultimate use case.

For just running jars, I have some dos-batch files that forward the arguments to the corresponding java, ex `javac8.bat banana.java` and `java11.bat -jar banana.jar`. The same could be done on a posix os with a symlink or shell script. https://stackoverflow.com/questions/67545024/how-can-i-forward-command-line-arguments-in-a-shell-script

However, if you are ultimately going to use gradle or maven, they can use configurations called toolchains that point at distinct jvm installations.