yamlmatlab icon indicating copy to clipboard operation
yamlmatlab copied to clipboard

Javaaddpath May Not Work Properly Due to Search Location

Open TheOriginalMrClean opened this issue 7 years ago • 1 comments

If the end file location is not in the current working directory of MATLAB, ReadYamlRaw() and WriteYaml() may not properly import snakeyaml because the javapath was not added correctly.

This is due to these lines:

 [pth,~,~] = fileparts(filename);
...

...
if not(ismember(dp, javaclasspath ('-dynamic')))
       javaaddpath(dp); % javaaddpath clears global variables...!?
end

Notice how the script is looking at the current directory of the file rather than the current working directory.

TheOriginalMrClean avatar Aug 10 '18 11:08 TheOriginalMrClean

The above issue can be fixed by the following code replacing the if statement above:

if not(ismember(dp, javaclasspath ('-dynamic')))
       javaaddpath(dp); % javaaddpath clears global variables...!?
            
elseif not(ismember(fullfile(pwd,'external\snakeyaml-1.9.jar'),javaclasspath ('-dynamic')))
       javaaddpath(fullfile(pwd,'external\snakeyaml-1.9.jar'));
end

I would recommend each user check this and if the user is going to be using ReadYaml() and WriteYaml() in a specific way (for instance, I will always have the ReadYaml() and WriteYaml() in a folder which the external folder containing snakeyaml-1.9.jar is inside. Therefore, the above code always should work.

I swapped the order which the code checks locations to improve speed for myself, but checking the output/input file directory first also works. Either way, I would recommend checking the working directory be standard implementation, while users be encouraged to add other locations for their various arrangements of files. If they so choose, they could also do a search for the file within or around the current working directory. However, that is obviously costly for processing time.

TheOriginalMrClean avatar Aug 10 '18 11:08 TheOriginalMrClean