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  {} {} javarmpath (@var{clspath})
28## @deftypefnx {} {} javarmpath (@var{clspath1}, @dots{})
29## @deftypefnx {} {} javarmpath (@{@var{clspath1}, @dots{}@})
30## Remove @var{clspath} from the dynamic class path of the Java virtual
31## machine.
32##
33## @var{clspath} may either be a directory where @file{.class} files are found,
34## or a @file{.jar} file containing Java classes.  Multiple paths may be
35## removed at once by specifying additional arguments, or by using a cell array
36## of strings.
37## @seealso{javaaddpath, javaclasspath}
38## @end deftypefn
39
40function javarmpath (varargin)
41
42  if (nargin < 1)
43    print_usage ();
44  endif
45
46  if (! all (cellfun (@(c) ischar (c) || iscellstr (c), varargin)))
47    error ("javarmpath: arguments must be strings or cell array of strings");
48  endif
49
50  for arg = varargin
51    if (iscellstr (arg{1}))
52      arg = arg{1}(:).';  # Guarantee cellstr array is a row vector
53    endif
54
55    for clspath = arg
56      clspath = clspath{1};
57
58      old_path = canonicalize_file_name (tilde_expand (clspath));
59      if (isfolder (old_path))
60        if (old_path(end) != filesep ())
61          old_path = [old_path, filesep()];
62        endif
63      endif
64
65      success = javaMethod ("removeClassPath", "org.octave.ClassHelper",
66                            old_path);
67
68      if (! success)
69        warning ("javarmpath: %s: not found in Java classpath", old_path);
70      endif
71    endfor
72  endfor
73
74endfunction
75
76
77## FIXME: These tests may fail if either TEMPDIR or HOME have already
78##        been added to the Java class path.
79
80## Basic test with single string
81%!testif HAVE_JAVA; usejava ("jvm")
82%! pth = tempdir ();
83%! unwind_protect
84%!   javaaddpath (pth);
85%!   clspth1 = javaclasspath ("-dynamic");
86%!   javarmpath (pth);
87%!   clspth2 = javaclasspath ("-dynamic");
88%!   assert (numel (clspth2), numel (clspth1) - 1);
89%!   assert (clspth2(1:end), clspth1(2:end));
90%! unwind_protect_cleanup
91%!   javarmpath (pth);
92%! end_unwind_protect
93
94## Remove two strings
95%!testif HAVE_JAVA; usejava ("jvm")
96%! pth1 = tempdir ();
97%! pth2 = tilde_expand ("~");
98%! unwind_protect
99%!   javaaddpath (pth1, pth2);
100%!   clspth1 = javaclasspath ("-dynamic");
101%!   javarmpath (pth1, pth2);
102%!   clspth2 = javaclasspath ("-dynamic");
103%!   assert (numel (clspth2), numel (clspth1) - 2);
104%!   assert (clspth2(1:end), clspth1(3:end));
105%! unwind_protect_cleanup
106%!   javarmpath (pth1, pth2);
107%! end_unwind_protect
108
109## Remove cell array of two strings
110%!testif HAVE_JAVA; usejava ("jvm")
111%! pth1 = tempdir ();
112%! pth2 = tilde_expand ("~");
113%! unwind_protect
114%!   javaaddpath (pth1, pth2);
115%!   clspth1 = javaclasspath ("-dynamic");
116%!   javarmpath ({pth1, pth2});
117%!   clspth2 = javaclasspath ("-dynamic");
118%!   assert (numel (clspth2), numel (clspth1) - 2);
119%!   assert (clspth2(1:end), clspth1(3:end));
120%! unwind_protect_cleanup
121%!   javarmpath (pth1, pth2);
122%! end_unwind_protect
123
124## Test input validation
125%!error <Invalid call> javarmpath ()
126%!error <arguments must be strings> javarmpath (5)
127%!error <arguments must be .* cell array of strings> javarmpath ({5})
128