Skip to content

Commit 19cff50

Browse files
committed
add java_api()
1 parent 05301c4 commit 19cff50

File tree

7 files changed

+53
-12
lines changed

7 files changed

+53
-12
lines changed

+stdlib/cpu_load.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
b = java.lang.management.ManagementFactory.getOperatingSystemMXBean();
1111

12-
L = b.getSystemCpuLoad(); % deprecated, but Matlab R2023b didn't have getCpuLoad() yet
12+
if stdlib.java_api() < 14
13+
L = b.getSystemCpuLoad();
14+
else
15+
L = b.getCpuLoad();
16+
end
17+
18+
% https://docs.oracle.com/en/java/javase/21/docs/api/jdk.management/com/sun/management/OperatingSystemMXBean.html#getSystemCpuLoad()
1319

1420
end

+stdlib/ini2struct.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
elseif Val(1)=='''' % single-quoted string
3333
Val = strtok(Val, '''');
3434
else
35-
Val = strtok(Val, ';'); % remove inline comment
36-
Val = strtok(Val, '#'); % remove inline comment
35+
Val = strtok(Val, ";"); % remove inline comment
36+
Val = strtok(Val, "#"); % remove inline comment
3737
Val = strtrim(Val); % remove spaces before comment
3838

39-
[val, status] = str2num(Val);
39+
[val, status] = double(string(Val));
4040
if status, Val = val; end % convert string to number(s)
4141
end
4242

+stdlib/java_api.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function api = java_api()
2+
3+
v = stdlib.java_version();
4+
5+
% major version is first number before "."
6+
7+
a = split(v, ".");
8+
if a(1) == "1"
9+
api = double(a(2));
10+
else
11+
api = double(a(1));
12+
end
13+
14+
end

+stdlib/java_version.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
% get version of Java Virtual Machine
33
v = string(java.lang.System.getProperty("java.version"));
44

5+
% this gives a long string with more detail
6+
% version("-java")
7+
58
% these give the Matlab version, not the JVM version.
69
% java.lang.Runtime.version()
710
% java.lang.Runtime.getRuntime().version

+stdlib/ram_total.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
function bytes = ram_total()
22
%% ram_total()
33
% get total physical RAM across operating systems
4-
% https://docs.oracle.com/en/java/javase/21/docs/api/jdk.management/com/sun/management/OperatingSystemMXBean.html#getTotalMemorySize()
4+
% https://docs.oracle.com/en/java/javase/21/docs/api/jdk.management/com/sun/management/OperatingSystemMXBean.html#getTotalPhysicalMemorySize()
55
%
66
%%% Outputs
77
% * bytes: total physical RAM [bytes]
88

9-
import java.lang.management.ManagementFactory
10-
11-
b = ManagementFactory.getOperatingSystemMXBean();
9+
b = java.lang.management.ManagementFactory.getOperatingSystemMXBean();
1210

13-
bytes = b.getTotalPhysicalMemorySize(); % deprecated but Matlab R2023b doesn't have getPhysicalMemorySize() yet
11+
bytes = b.getTotalPhysicalMemorySize();
12+
% deprecated but Matlab doesn't have Java 14 getPhysicalMemorySize() yet
13+
% https://docs.oracle.com/en/java/javase/21/docs/api/jdk.management/com/sun/management/OperatingSystemMXBean.html#getTotalMemorySize()
1414

1515
end

+stdlib/version_atleast.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
% r: logical
1313

1414
arguments
15-
in (1,1) string
16-
ref (1,1) string
15+
in (1,1) string
16+
ref (1,1) string
1717
end
1818

1919
in = split(in, ' ');

Readme.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ buildtool
1717

1818
[API Documentation](https://geospace-code.github.io/matlab-stdlib)
1919

20-
## Developer notes
20+
## Java details
2121

2222
The "matlab-stdlib" package uses Java functions throughout, with the higher-level
2323
[java.nio.Files](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/Files.html)
@@ -28,6 +28,24 @@ That is, if Matlab was started with
2828
[-nojvm](https://www.mathworks.com/help/matlab/matlab_env/commonly-used-startup-options.html),
2929
many of the stdlib filesystem function do not work.
3030

31+
Get the JVM version with
32+
33+
```matlab
34+
version("-java")
35+
```
36+
37+
The Matlab default
38+
[JVM can be configured](https://www.mathworks.com/help/matlab/matlab_external/configure-your-system-to-use-java.html)
39+
to
40+
[compatible JRE](https://www.mathworks.com/support/requirements/language-interfaces.html)
41+
across
42+
[Matlab versions](https://www.mathworks.com/support/requirements/openjdk.html)
43+
by using the
44+
[jenv](https://www.mathworks.com/help/matlab/ref/jenv.html)
45+
Matlab function.
46+
47+
Matlab-stdlib has been test with JVM from version 8 to 17.
48+
3149
## Acknowledgements
3250

3351
Stdlib for Matlab was partly funded by NASA NNH19ZDA001N-HDEE grant 80NSSC20K0176.

0 commit comments

Comments
 (0)