Switching between versions

I’ve been cleaning up my development laptop - replacing sdkman, asdf and others with just flox.

Everything’s going nicely, except for one use case where I don’t see the best way forward. With sdkman, I could use it to easily switch between Java 11 and Java 17 in the same shell. How would I achieve something like this with flox?

Two separate environments attached to the same directory ? Is that even possible?
A scripted edit of the TOML manifest file followed by an update/upgrade ?

Thanks!

welcome to the community Dewi an interesting question–what’s the main motivator behind switching java versions like this (if you don’t mind me asking)?

my first idea of a workaround is you could put the Java {other version} in another directory or the FloxHub and you could activate it directly with flox activate -d /some/other/location or flox activate -r yourgithubuser/java-different-version-env

I have some Java projects under ~/projects/java. I started by creating a flox environment there with the usual Java tooling : openjdk, maven, gradle etc.

Let’ say I’m usually developing under Java 17. But I might want to change to 21 briefly in order to see if my 17 bytecode runs there, or to build the project under 21 in order to see what issues exist there.

I have no problem with separate complete environments for 7 and 21. It’s the way that works with flox layering that’s confusing me. My first impression from the docs was that an environment was something bound to a directory, and automatically activating the environment on a cd would be a good thing.

Would best practice be something like this:

  • Have ~/projects/java/java-17 and ~/projects/java/java-21 directories with their own environments. They’d be bare apart from a .flox directory.
  • cd to ~/projects/java/myproject and do “flox activate - d …/java-17”

I daresay I could find use cases for the same kind of trick with g++ and clang.

Thanks for the feedback - it helps me keep digging!

Those methods look like they could work! we also have this issue for better auto-activation on cd - configurable auto activate / exit environment behavior · Issue #688 · flox/flox · GitHub

Thanks for the encouragement!

1 Like