Configure Java AWT and Java Swing in Matlab Docker Image
11 views (last 30 days)
Show older comments
To make a custom style cheker that we want to run in Jenkins as one of a auto-test we use:
function indentedText = indentcode(text,language)
it is needed to get right indentation in the code.
This function utilizes something undocumented interacting with Java Swing :
function indentedText = indentcode(text,language)
%indentcode Indents code.
% indentcode(T) indents text T according to user's preferences specified
% for the MATLAB language. Indented text retains the same line separator
% style as specified by the input text.
%
% indentcode(T,L) specifies language L and must be one of the following:
% 'c', 'java', 'matlab', 'plain', 'simscape', 'tlc', 'verilog',
% 'vhdl', 'xml'.
%
% This file is for internal use only and is subject to change without
% notice.
% Copyright 2012 The MathWorks, Inc.
error(javachk('swing'));
narginchk(1,2);
if ~ischar(text)
error(message('MATLAB:INDENTCODE:NotString'));
end
if nargin < 2
language = 'matlab';
end
if strcmp(text, '')
indentedText = text;
return
end
switch language
case 'c'
langInst = com.mathworks.widgets.text.cplusplus.CLanguage.INSTANCE;
case 'java'
langInst = com.mathworks.widgets.text.java.JavaLanguage.INSTANCE;
case 'matlab'
langInst = com.mathworks.widgets.text.mcode.MLanguage.INSTANCE;
case 'simscape'
langInst = com.mathworks.widgets.text.simscape.SimscapeLanguage.INSTANCE;
case 'xml'
langInst = com.mathworks.widgets.text.xml.XMLLanguage.INSTANCE;
otherwise
error(message('MATLAB:INDENTCODE:InvalidLanguage'));
end
indentedText = char(com.mathworks.widgets.text.EditorLanguageUtils.indentText(...
langInst,text));
end
It works fine in a normal Matlab with GUI, but does not work inside the Matlab Docker Image.
We tried to install Java into the Matlab Docker Image, but it did not help.
Here is a question, is it possible to run this function inside the Matlab Docker Image?
Thank you!
1 Comment
Daniel
on 5 Nov 2022
Hi Angelina, did you find a solution to your question? I'd be curious to know. Thanks, Daniel
Answers (1)
Swastik Sarkar
on 15 Mar 2024
It seems you're attempting to use the 'indentcode' function within a MATLAB Docker image but are encountering the ‘Swing is currently not available’ error.
This issue typically arises because MATLAB, when run without a display, does not load all Java Libraries. You can verify which Java libraries are loaded with the 'javachk' command.
To address this, I recommend a workaround that involves simulating an X display to ensure all Java Libraries are loaded. Here's how you can proceed:
- Start by getting a shell to your Docker container. Use the -shell flag with your docker image command like so:
docker run -it -- rm -p 8888:8888 --shm-size=512M mathworks/matlab:r2021b -shell
2. Once in the shell, update your package lists and install OpenJDK and Xvfb (X virtual framebuffer):
sudo apt update
sudo apt install openjdk-17-jdk xvfb
3. After installing the dependencies, initiate a dummy X Display server. In this example, we'll use port :99:
sudo Xvfb :99 &
4. With the X Display server running, you can now execute the ‘indentcode’ function in MATLAB, specifying the display to use:
matlab -batch "indentcode('disp(a);')" -display :99
Hope this helps you use ‘indentcode’ in your Docker container and smoothly integrate with Jenkins.
0 Comments
See Also
Categories
Find more on Java Package Integration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!