MATLAB Answers

How do I provide a "string input" via the command line to a jar built with Matlab’s JavaBuilder in Ubuntu using MCR libraries?

6 views (last 30 days)
Here a simple Matlab function that takes a String as input.
function dispStr(input_string)
fprintf('This is my input %s\n',input_string);
end
Next I'll compile it to get the corresponding dispStr.jar.
  1. Apps >> Library Compiler
  2. Type: Java Package
  3. Exported functions (main file): dispStr.m
  4. Now create the “package”
Test that it runs in Windows:
C: >java classpath "C:\MATLAB\R2014b\toolbox\javabuilder\jar\javabuilder.jar";C:\MATLAB\R2014b\work\dispStr\for_testing\dispStr.jar dispStr.Class1 'test'
This returns: This is my input test
Now we will test the “string input” using the MCR on an Ubuntu system. We assume that the MCR has been installed and tested with the magicsqr package successfully (see here for details).
Copy the for_testing folder to a shared folder (between Windows and Ubuntu VM).
Execute the following commands:
sudo -i
export LD_LIBRARY_PATH="/opt/MCR/v84/runtime/glnxa64:/opt/MCR/v84/bin/glnxa64:/opt/MCR/v84/sys/os/glnxa64:${LD_LIBRARY_PATH}"
export XAPPLRESDIR="/opt/MCR/v84/X11/app-defaults"
java -classpath "/opt/MCR/v84/toolbox/javabuilder/jar/javabuilder.jar:/media/sf_shared/for_testing/dispStr.jar" dispStr.Class1 'test'
This returns the following exception:
Error using eval
Undefined function or variable 'test'.
com.mathworks.toolbox.javabuilder.MWException: Undefined function or variable 'test'.
Exception caught: java.lang.NullPointerException
Note: You would get the same error in Windows if you use double quotes for the string input, e.g. dispStr.Class1 "test".
Is there a different way to pass string variables in Ubuntu?

Accepted Answer

Wolfgang Garn
Wolfgang Garn on 23 Jun 2015
Edited: Wolfgang Garn on 23 Jun 2015
There is indeed another way of passing String variables: \'my-connected-string\'. One needs to use a backslash and be careful with space characters.
java -classpath "/opt/MCR/v84/toolbox/javabuilder/jar/javabuilder.jar:/media/sf_shared/for_testing/dispStr.jar" dispStr.Class1 \'this-is-how-it-works\'
This is my input this-is-how-it-works
Now the next question is how do I pass a string with spaces: you need to use a backslash followed by a space:
java -classpath "..." dispStr.Class1 \'this\ is\ how\ it\ works\'
How do you pass a folder/file.
java -classpath "..." dispStr.Class1 \'\/my\/path\/to\/a_file.txt\'
It would be great if anyone knew where there is a complete list of these syntax conventions.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!