LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
lineDownloadJAR.m
1function jar_path = lineDownloadJAR(verbose)
2% LINEDOWNLOADJAR Download jline.jar from SourceForge if not found locally.
3%
4% jar_path = lineDownloadJAR()
5% jar_path = lineDownloadJAR(verbose)
6%
7% Downloads jline.jar from SourceForge (https://line-solver.sourceforge.net/latest/jline.jar)
8% and places it in the common/ directory (auto-discovered or current repo).
9% Returns the path to the JAR after download.
10%
11% Args:
12% verbose (optional): If true, print status messages (default: true)
13%
14% Returns:
15% jar_path: Full path to jline.jar
16%
17% Usage:
18% jar_path = lineDownloadJAR(); % Auto-download and set up
19% jar_path = lineDownloadJAR(false); % Silent mode
20
21if nargin < 1 || isempty(verbose)
22 verbose = true;
23end
24
25% 1. Try to locate existing jline.jar
26existing_jar = which('jline.jar');
27if ~isempty(existing_jar) && isfile(existing_jar)
28 if verbose
29 fprintf('jline.jar already found at: %s\n', existing_jar);
30 end
31 jar_path = existing_jar;
32 return;
33end
34
35% 2. Try to find/create common/ directory
36repo_root = lineGetRepoRoot();
37if isempty(repo_root)
38 repo_root = pwd;
39end
40
41common_dir = fullfile(repo_root, 'common');
42if ~isdir(common_dir)
43 mkdir(common_dir);
44 if verbose
45 fprintf('Created directory: %s\n', common_dir);
46 end
47end
48
49jar_path = fullfile(common_dir, 'jline.jar');
50
51% 3. Check if already downloaded to common/
52if isfile(jar_path)
53 if verbose
54 fprintf('jline.jar found in common/: %s\n', jar_path);
55 end
56 addPathIfNeeded(common_dir);
57 return;
58end
59
60% 4. Get version from GlobalConstants.java (works on all OS)
61repo_root = lineGetRepoRoot();
62gc_file = fullfile(repo_root, 'jar', 'src', 'main', 'java', 'jline', 'GlobalConstants.java');
63
64version = '';
65if isfile(gc_file)
66 try
67 content = fileread(gc_file);
68 % Extract version string: Version = "X.Y.Z"
69 % Use character arrays for cross-platform compatibility
70 tokens = regexp(content, 'Version\s*=\s*"([^"]+)"', 'tokens');
71 if ~isempty(tokens) && ~isempty(tokens{1})
72 version = char(tokens{1}{1});
73 end
74 catch
75 % Silently continue if version extraction fails
76 end
77end
78
79% 5. Download versioned jar from SourceForge
80if isempty(version)
81 % Fallback: download generic jline.jar if version detection fails
82 versioned_jar_path = '';
83 jar_url = 'https://line-solver.sourceforge.net/latest/jline.jar';
84 jar_filename = 'jline.jar';
85 if verbose
86 fprintf('Version detection failed; will download generic jline.jar\n');
87 end
88else
89 jar_filename = sprintf('jline-%s.jar', version);
90 versioned_jar_path = fullfile(common_dir, jar_filename);
91 jar_url = sprintf('https://line-solver.sourceforge.net/latest/%s', jar_filename);
92end
93
94if verbose
95 fprintf('Downloading %s from: %s\n', jar_filename, jar_url);
96 fprintf('Target: %s\n', jar_path);
97end
98
99try
100 % Check if versioned jar already exists (cross-platform)
101 if ~isempty(versioned_jar_path) && isfile(versioned_jar_path)
102 if verbose
103 fprintf('Found existing %s\n', jar_filename);
104 end
105 if ~isfile(jar_path)
106 % Copy versioned jar to generic jline.jar (works on all OS)
107 copyfile(versioned_jar_path, jar_path);
108 if verbose
109 fprintf('Linked: %s -> jline.jar\n', jar_filename);
110 end
111 end
112 else
113 % Download from SourceForge (websave works on all OS)
114 options = weboptions('Timeout', 60, 'ContentType', 'auto');
115 websave(jar_path, jar_url, options);
116
117 if ~isfile(jar_path)
118 error('lineDownloadJAR:DownloadFailed', 'Download completed but file not found');
119 end
120
121 file_info = dir(jar_path);
122 if verbose
123 fprintf('Downloaded jline.jar (%.1f MB)\n', file_info.bytes / (1024*1024));
124 end
125
126 % If we downloaded versioned jar, also copy to common name
127 if ~isempty(versioned_jar_path) && ~isequal(jar_path, versioned_jar_path)
128 copyfile(jar_path, versioned_jar_path);
129 if verbose
130 fprintf('Also saved as: %s\n', jar_filename);
131 end
132 end
133 end
134
135 if ~isfile(jar_path)
136 error('lineDownloadJAR:DownloadFailed', 'Failed to set up jline.jar');
137 end
138
139 % Add to MATLAB path if not already there
140 addPathIfNeeded(common_dir);
141
142catch ME
143 % Clean up partial files if download failed (safe on all OS)
144 if isfile(jar_path)
145 try
146 delete(jar_path);
147 catch
148 end
149 end
150 if ~isempty(versioned_jar_path) && isfile(versioned_jar_path)
151 try
152 delete(versioned_jar_path);
153 catch
154 end
155 end
156
157 error('lineDownloadJAR:DownloadFailed', ...
158 ['Failed to download jline.jar from SourceForge:\n', ...
159 ME.message, '\n\n', ...
160 'Options:\n', ...
161 '1. Download manually: https://line-solver.sourceforge.net/latest/jline.jar\n', ...
162 '2. Build locally: cd jar && mvn clean package -P b\n', ...
163 '3. Set JLINE_JAR environment variable to point to existing jar']);
164end
165
166end
167
168% -----------------------------------------------------------------------
169function repo_root = lineGetRepoRoot()
170% Try to find the LINE repository root by searching for key markers.
171repo_root = '';
172
173% Start from this file's location
174this_file = mfilename('fullpath');
175search_dir = fileparts(fileparts(fileparts(this_file)));
176
177% Walk up looking for markers (jar/, matlab/, python/ subdirs)
178for depth = 0:5
179 if isfolder(fullfile(search_dir, 'jar')) && ...
180 isfolder(fullfile(search_dir, 'matlab')) && ...
181 isfolder(fullfile(search_dir, 'python'))
182 repo_root = search_dir;
183 return;
184 end
185 parent = fileparts(search_dir);
186 if strcmp(parent, search_dir)
187 break;
188 end
189 search_dir = parent;
190end
191
192end
193
194% -----------------------------------------------------------------------
195function addPathIfNeeded(dir_path)
196% Add directory to MATLAB path if not already there
197current_paths = path;
198if ~contains(current_paths, dir_path)
199 addpath(dir_path);
200end
201end
Definition Station.m:245