I have an .m file in MATLAB which I would like to call from Java an get the solution as a string or whatever in Java. This sounds really simple but for some reason I can't make it work.
I tried this:
matlab -nosplash -wait -nodesktop -r myFunction
but I'm not sure how I parse the answer since MATLAB opens it's own command line (in Windows).
I use this, but it doesn't return anything.
Process p = Runtime.getRuntime().exec(commandToRun);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
also it seems that every time I call MATLAB it opens a separate window which is a problem because I'd like to run this many times.
The trick is to use the MatlabControl class http://www.cs.virginia.edu/~whitehouse/matlab/JavaMatlab.html. It's very easy to use and you can do exactly what you're trying to do (and more).
matlabcontrol is based on the same underlying MATLAB library used by MatlabControl mentioned by Jeff, but is more up to date, reliable, and documented. To get started, take a look at the walkthrough.
In Matlab R2016b, MathWorks added MATLAB Engine API for Java which allows to execute MATLAB code from Java.
JAMAL is an open source, Java RMI-based (Java Remote Method Invocation API) library that suits your needs
There exists a good Java-COM-Bridge called JaCoB (http://sourceforge.net/projects/jacob-project/) which you can use to automatically start Matlab as a COM-Server in the background. You can then follow the instructions in the Matlab help to interact with the Matlab COM Interface.
Although this is a very generic interface, it provides enough flexibility to easily do a few calls to Matlab like in your case.
Simply download the JaCoB package and look in the docs folder for some documentation.
You also have to include the Jacob DLL in your path.
Related
I need to access the windows native file upload and save window using java.
Though it is not possible only using java so I try this using jacob and JNA library.
Actually I also need to handle the any type of pop up message from OS.
There are lots of any other approach using java robot,sikuli,autoit but i want to stick(learn)
on jacob or jna.
Can any body help me regarding this problem.Any code snippet on this, helpful for me not only that if some body told me how to get hwnd ID dword ID which is need to pass into the different methods that also be useful for me.
Im guessing you were referring to CFileDialog or OpenFileDialog, well that can be accomplished via JNI and a native DLL which you need to compile yourself, which is somewhat easy:
create a C++ DLL - project in visual studio, i'd recommend using MFC - methods which are called via C-functions, that'll make everything MUCH easier since you wont need to re-invent the wheel and create WindowProcs yourself and whatnot - MFC does all of that transparently. You could also create a CLR-DLL which uses WinForms but thats a bit more complex. Do yourself a favor and disregard anything about MinGW, GCC or even any other OSS-solution if you want to create native windows ... that'll give you headaches, trust me. Within windows, you will need to use microsoft-libraries if you want to create Microsoft-compatible user interfaces, everything else is very error-prone.
include jni.h which is found in your JDK-library
write a JNI-compatible method signature
compile
declare compatible, native methods in java
compile
try to run your java project
report back, i shall help you to some extent if you run into any problems
JNI howto
Could anyone please tell me how to make a symbolic link (in the same way MKLINK does) and/or remove a symbolic link with Java. I have found solutions that use Java as a wrapper and use a Windows native program to accomplish this, but I really want a pure Java solution. Thank you in advance!
Since Java 7 you can do this easily using the NIO package.
Path target = Paths.get("target");
Path link = Paths.get("link");
Files.createDirectory(target);
Files.createSymbolicLink(link, target);
Do remember that you do need the correct privileges for this. In my unit test I had to run eclipse as an administrator to make it work (same as that I couldn't create a link from a normal cmd.exe)
As far as I know window does not have real symbolic links like Unix-like system do.
However Windows has the following relevant tools:
You can map network drive, i.e. attach drive letter to specified network path. You can definitely do this using WMI. To access WMI from java take a look on tools like JaWin, Jinterop, Jintegra or write WMI script in JScript o VBScript and execute is from Java.
You can use command subst that assigns letter to local file system path. This is the closest approach to Unix soft link.
You can create desktop shortcut. Create one manually and take a look on it. Shortcut is actually regular text file (as far as I remember in INI format). You can easily create one using any language you want including java. This is not soft link but it is clickable.
I am currently making a project for school, where I am going to make a program which teaches children how to read. My basic idea for the program was produce the sentence and then get Windows Anna to say it. My question to you is, how can I access Winodws Anna through Java? and is there a better way of doing this?
Thanks
If having the program access internet is acceptable, then you could use iSpeech.
You can use their API, but the problem with that is that it is limited to 200 uses/day.
iSpeech has decently sounding voices, generally more polished than other TTS engines I've tired like espeak or FreeTTS, because it actually pronounces the words more fluently. Sure, it might pronounce 'Wind', relating with air, as 'Wind', relating to twisting, but other than that, it speaks quite well.
Also, while I haven't had any prior experience with this, I found an article that shows you how to access the MS Speech with command line (which can obviously be commanded through Java[if you do not know how, here is a good article]). It is located here. In command line, all you do is type in 'SayDynamic.exe* the text you want to speak".
*Or SayStatic, the other download available on the page.
This method seems to be better in terms of speed and not relying on internet access, but it definitely does NOT pronounce things as well as iSpeech. I guess the ideal thing for your program to have would be to use iSpeech when online, and use the Say*.exe when offline.
The site also provides the source code of the program. As you might notice, it is NOT Microsoft Anna's voice, but you can specify that in the source and recompile it.
Hope I helped!
You can use command line utiity NirCmd that uses text-to-speech API installed on Windows.
So, supply this utility together with your java application and run it with appropriate command line.
You can try FreeTTS : a speech synthesizer written in java.
You can try to call the Microsoft Speech API (SAPI) but I don't know how to do it in java.
Can you tell us how you invoke NirCmd ?
Altenatively to NirCmd, you can build your own tool in C# that will read the text. The text could be within a txt and your tool invoked with the path to that txt as argument. You can easily adapt a demo project like this one : http://www.codeproject.com/Articles/19334/Text-to-Speech-using-Windows-SAPI
There is the Speech platform of Windows
http://www.microsoft.com/en-us/download/details.aspx?id=27226
The Speech runtime
http://www.microsoft.com/en-us/download/details.aspx?id=27225
You can use JNA (not JNI) to interact with dll from java
https://github.com/twall/jna
Is it possible to start other application that are installed on the system with my java app and pass a file as a parameter to them? I have a client which receives videos from a server and I want my client program to start, lets say the VLC player with the file that I received. How do I manage to do that?
Use Desktop#open(). It will launch the platform default associated application to open the given file.
File file = new File("/absolute/path/to/file.vlc");
Desktop.getDesktop().open(file);
No need to hassle with Runtime#exec() or ProcessBuilder for which you would have to add platform detection and to write platform specific logics for.
Quite simply:
Runtime.getRuntime().exec("vlc [arguments]"); //Write all arguments as you would in your shell.
Make sure you catch all relevant exceptions
You can call the exec method on the Runtime object.
Runtime.getRuntime().exec("System specific command line text here");
You can run an external program pretty easily on Java 5+ with ProcessBuilder, including passing arguments and handling input/output streams.
eg.
ProcessBuilder movieProcess = new ProcessBuilder("/path/to/movieplayer", "/path/to.moviefile");
movieProcess.start();
Only used it myself executing non-UI stuff, I'll give it a quick go and see what happens with something like VLC.
Update - works a treat for flv on Ubuntu, UI is visible and accepts file arguments.
I can call a .m file in Matlab from Java and that works fine, but I want to retrieve the results from Matlab and display them in Java. How can I do this?
matlabcontrol is a Java API which will allow you to do just that. It allows for invoking eval and feval in MATLAB and returning the results to MATLAB. The walkthrough explains with examples how to do this. The walkthrough uses built-in MATLAB functions and commands, but you can use it with your own .m files because using matlabcontrol is just like interacting with MATLAB's Command Window.