Issue setting "default" Java version

I'm on FreeBSD 13.1, and I'm trying to set the default Java version when I run /usr/local/bin/java to be 17, however I also need JDK 8 to be installed for one application that I configured manually (and so was much easier to update the init script for just that one to specify JDK 8 than it would be to do so repeatedly for package-based apps which don't work with JDK 8 and require something newer).

My understanding is that the executable symlinked there is just a wrapper which is supposed to just use whichever runtime is listed first in /usr/local/etc/javavms, but that doesn't seem to be what's happening for me:

Code:
kreeblah@bigwhoop:~ $ which java
/usr/local/bin/java
kreeblah@bigwhoop:~ $ java -version
openjdk version "1.8.0_352"
OpenJDK Runtime Environment (build 1.8.0_352-b08)
OpenJDK 64-Bit Server VM (build 25.352-b08, mixed mode
kreeblah@bigwhoop:~ $ env JAVA_VERSION=17 java -version
openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment (build 17.0.5+8-1)
OpenJDK 64-Bit Server VM (build 17.0.5+8-1, mixed mode, sharing)
kreeblah@bigwhoop:~ $ env JAVA_VERSION=8 java -version
openjdk version "1.8.0_352"
OpenJDK Runtime Environment (build 1.8.0_352-b08)
OpenJDK 64-Bit Server VM (build 25.352-b08, mixed mode)
kreeblah@bigwhoop:~ $ cat /usr/local/etc/javavms
/usr/local/openjdk17/bin/java
/usr/local/openjdk8/bin/java

I have /usr/local/openjdk17/bin/java listed first, which I thought would mean that JDK 17 would be selected if I don't specify one, but it seems it's still pulling JDK 8. Am I doing something wrong here?
 
kreeblah@bigwhoop:~ $ cat /usr/local/etc/javavms
/usr/local/openjdk17/bin/java
/usr/local/openjdk8/bin/java


I have /usr/local/openjdk17/bin/java listed first, which I thought would mean that JDK 17 would be selected if I don't specify one,
The list order in that file doesn't determine the version to be selected first, the file provides only a list of registered java vms:

javavms(5)
Code:
DESCRIPTION
     The javavms file contains a list of the registered Java VMs which are
     available to be used by javavm.  Each Java VM is listed by specifying the
     location of the main “java” executable on a line.
     ...

FILES
     /usr/local/etc/javavms
             The location of the Java VM wrapper configuration file.

According to javavm(1) the default version selection is determined differently.
Rich (BB code):
DESCRIPTION
     ...
     By default, javavm will select the most “native” and up to date version
     of the Java VM when a given symbolic link is used, invoking and passing
     the arguments to the matching executable within the chosen Java VM.  The
     choice of Java VM may also be influenced by using environment variables
     to constrain the version, vendor and operating system of the Java VM.

     This selection process is usually achieved through the use of
     /usr/ports/Mk/bsd.java.mk.  However, if this is not present then javavm
     will use its own internal selection process which is designed to behave
     almost identically.
Have you a updated ports tree installed under /usr/ports? If not, maybe the internal selection process of javavm isn't working as expected. The java/javavmwrapper port, which provides javavm, was updated last on 2022-07-20.

Note: /usr/local/bin/java is a symlink to javavm:
Code:
% ls -l /usr/local/bin/java
lrwxr-xr-x  1 root  wheel  21 Nov 24 12:01 /usr/local/bin/java -> /usr/local/bin/javavm
 
The list order in that file doesn't determine the version to be selected first, the file provides only a list of registered java vms:

javavms(5)
Code:
DESCRIPTION
     The javavms file contains a list of the registered Java VMs which are
     available to be used by javavm.  Each Java VM is listed by specifying the
     location of the main “java” executable on a line.
     ...

FILES
     /usr/local/etc/javavms
             The location of the Java VM wrapper configuration file.

According to javavm(1) the default version selection is determined differently.
Rich (BB code):
DESCRIPTION
     ...
     By default, javavm will select the most “native” and up to date version
     of the Java VM when a given symbolic link is used, invoking and passing
     the arguments to the matching executable within the chosen Java VM.  The
     choice of Java VM may also be influenced by using environment variables
     to constrain the version, vendor and operating system of the Java VM.

     This selection process is usually achieved through the use of
     /usr/ports/Mk/bsd.java.mk.  However, if this is not present then javavm
     will use its own internal selection process which is designed to behave
     almost identically.
Have you a updated ports tree installed under /usr/ports? If not, maybe the internal selection process of javavm isn't working as expected. The java/javavmwrapper port, which provides javavm, was updated last on 2022-07-20.

Note: /usr/local/bin/java is a symlink to javavm:
Code:
% ls -l /usr/local/bin/java
lrwxr-xr-x  1 root  wheel  21 Nov 24 12:01 /usr/local/bin/java -> /usr/local/bin/javavm

It looks like /usr/ports/Mk/bsd.java.mk was what I was looking for. It seems that there's a preference list in there, with JDK 8 before JDK 17. I've been running off of the quarterly packages, but maybe I can update that makefile to generate a package with a different preference list. It doesn't seem to be updated that often.

Thanks!

Edit: Seems I don't even need to build a new package. It appears to be included at runtime.
 
It looks like /usr/ports/Mk/bsd.java.mk was what I was looking for. It seems that there's a preference list in there, with JDK 8 before JDK 17. I've been running off of the quarterly packages, but maybe I can update that makefile to generate a package with a different preference list. It doesn't seem to be updated that often.
I *think* that should be set by the port developers. JDK8 is certainly dated and that should be bumped to 11 at the very least.

My 2-cents would be to either set it in:
/usr/local/etc/javavm_opts.conf

or per user in your profile as Alain has setup. I have been doing per user, but want to have it system-wide and don't necessarily want to change the port preference.
 
Back
Top