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

5 ビュー (過去 30 日間)
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?

採用された回答

Wolfgang Garn
Wolfgang Garn 2015 年 6 月 23 日
編集済み: Wolfgang Garn 2015 年 6 月 23 日
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.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall MATLAB from Java についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by