1########################################################################
2##
3## Copyright (C) 2007-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {} javaclasspath ()
28## @deftypefnx {} {@var{dpath} =} javaclasspath ()
29## @deftypefnx {} {[@var{dpath}, @var{spath}] =} javaclasspath ()
30## @deftypefnx {} {@var{clspath} =} javaclasspath (@var{what})
31## Return the class path of the Java virtual machine in the form of a cell
32## array of strings.
33##
34## If called with no inputs:
35##
36## @itemize
37## @item If no output is requested, the dynamic and static classpaths are
38## printed to the standard output.
39##
40## @item If one output value @var{dpath} is requested, the result is the
41## dynamic classpath.
42##
43## @item If two output values@var{dpath} and @var{spath} are requested, the
44## first variable will contain the dynamic classpath and the second will
45## contain the static classpath.
46## @end itemize
47##
48## If called with a single input parameter @var{what}:
49##
50## @table @asis
51## @item @qcode{"-dynamic"}
52## Return the dynamic classpath.
53##
54## @item @qcode{"-static"}
55## Return the static classpath.
56##
57## @item @qcode{"-all"}
58## Return both the static and dynamic classpath in a single cellstr.
59## @end table
60## @seealso{javaaddpath, javarmpath}
61## @end deftypefn
62
63function [path1, path2] = javaclasspath (what = "")
64
65  if (nargin > 1)
66    print_usage ();
67  endif
68
69  ## dynamic classpath
70  dynamic_path = javaMethod ("getClassPath", "org.octave.ClassHelper");
71  dynamic_path_list = ostrsplit (dynamic_path, pathsep ());
72
73  ## static classpath
74  static_path = javaMethod ("getProperty",
75                            "java.lang.System", "java.class.path");
76  static_path_list = ostrsplit (static_path, pathsep ());
77  if (numel (static_path_list) > 1)
78    ## remove first element (which is .../octave.jar)
79    static_path_list(1) = [];
80  else
81    static_path_list = {};
82  endif
83
84  if (nargout == 0)
85    if (! nargin)
86      what = "-all";
87    endif
88    switch (tolower (what))
89      case "-dynamic", disp_path_list ("DYNAMIC", dynamic_path_list);
90      case "-static",  disp_path_list ("STATIC", static_path_list);
91      case "-all"
92        disp_path_list ("STATIC", static_path_list);
93        disp ("");
94        disp_path_list ("DYNAMIC", dynamic_path_list);
95      otherwise
96        error ("javaclasspath: invalid value for WHAT");
97    endswitch
98
99  else
100    if (! nargin)
101      ## This is to allow retrieval of both paths in separate variables with
102      ## a single call to javaclasspath().  Matlab returns only the -dynamic
103      ## path in this case but this won't break compatibility.
104      path1 = cellstr (dynamic_path_list);
105      path2 = cellstr (static_path_list);
106    else
107      switch (tolower (what))
108        case "-all",     path1 = cellstr ([static_path_list,dynamic_path_list]);
109        case "-dynamic", path1 = cellstr (dynamic_path_list);
110        case "-static",  path1 = cellstr (static_path_list);
111        otherwise
112          error ("javaclasspath: invalid value for WHAT");
113      endswitch
114    endif
115  endif
116
117endfunction
118
119## Display cell array of paths
120
121function disp_path_list (what, path_list)
122  printf ("   %s JAVA PATH\n\n", what);
123  if (numel (path_list) > 0)
124    printf ("      %s\n", path_list{:});
125  else
126    printf ("      - empty -\n");
127  endif
128endfunction
129