MATLAB-based Java program - java

I am in the process of designing a MATLAB-based algorithm. Is it possible to call that MATLAB source code from a Java application?
Has anyone come across such a issue?

have a look at MATLAB Builder JA. This program allows you to convert your matlab code into java classes.

It is possible to call MATLAB from Java using matlabcontrol. It's a Java API designed specifically for that. However, it requires MATLAB to be running on the same machine that the Java code is executing on. As you put the Android tag I assume you want to run this on an Android device (although not mentioned in your description), and there is no known way to directly run MATLAB code on Android. matlabcontrol definitely will not do that, but you could use it to write a Java application which acts as a server for your Android application. Similarly, you could do the same with MATLAB Builder JA which generates code that needs either MATLAB or the MATLAB Compiler Runtime (which is available for free - so users would not need to buy MATLAB).

Related

Calling a Matlab script from Java without license

I've implemented a Java-App which collects data. The app should call a Matlab script which does it's calculation with the collected data and delivers a result. How to do this?
And is there a way, that the user can use the Java-App from his own computer without a Matlab license?
I've got absolutely no idea about Matlab. It's only my task to call the script from my Java-App with the generated data as input.
Thank you for any help!
You can use Matlab Runtime to run compiled Matlab code on mashines that has no Matlab installation. Here is a Getting Started. The generation of the compiled code still needs a valid Matlab license.
Another alternative that requires no license is Octave which can be also used from Java however the syntax is a bit different.
Finally you can take a look on MatConsoleCtl -which was forked from the good old matlabcontrol- provides a really convenient usage to execute commands in a Matlab instance from Java. This approach starts a Matlab instance that requires a license again.

How to package a python script with my java program?

So, I'm writing a program(mostly for practice, kind of for future convenience) that will show a java front-end when run, and depending on the input, will use a python script to crawl and interpret a webpage, then send the interpreted data back to java to format for the front end. I'm sure I can get this running on my own computer, but when I want to distribute my program for my friends to use, how can I ensure my java program, mainProgram.jar can find my python code, script_x.py. Is there a way I can assure my python script will be supported. I've read that there are ways to include the necessary runtimes and such with your program when you build it, but would it still interact the same way with my java program if it was bundled with it's dependencies? I can provide some sample code soon, but it's still a work in progress, as I don't have the python script finished yet.
here is a link https://bytes.com/topic/python/insights/949995-three-ways-run-python-programs-java
use Jython (compiles to java byte code) https://en.wikipedia.org/wiki/Jython
distributing Jython applications: https://wiki.python.org/jython/JythonFaq/DistributingJythonScripts

How to write a Java program which will compile, load, and use code from the web

I'm looking to write a Java program which will download a Java source file in text format off the web, compile it, load it, and use it as part of the running program. I've heard this is possible, but don't know how to write the code to make it happen. A fully functioning example or tutorial would be great, if you could point me in the direction of documentation such as this.
Once I learn how it's done, I plan to use this knowledge to build an Android Application which can customize itself with code from the web.
A desktop program could use a shell script to download, compile and run a Java program. Android does not have a compiler, and adding one is non-trivial. The easiest way would be to also make a server program. The Android program would then tell the server program to download and compile the Java source code, and then send the result to the Android program, which would then load it using its ClassLoader.
One caveat is that the JDK compiler produces bytecode for the standard Java Virtual Machine, whereas Android's JVM is uses the Dalvik VM, so when you compile the Java class, you can't just use the JDK; you have to use the Android SDK to produce compatible bytecode that the Android ClassLoader can use.
Yes, it's possible in the general sense to do what you want. In the specific case of Android, however, it is likely that the sandbox imposes restrictions that will make what you're trying to do difficult or impossible. To do this in the general case you can use an approach like:
Use a web library of your choosing (for example, HttpClient, or HtmlUnit, or for simple tasks, Java's built-in URLConnection class is entirely acceptable as well) to download the Java file locally.
Use System.exec() to fork a javac process to compile the downloaded Java file for you (or use a JavaCompiler implementation to do the same). Note that this might be a bit tricky if the downloaded Java file uses external JAR's/libraries that aren't on your system.
Use the ClassLoader to load your compiled class. Note that you'll only be able to use it if your runtime classpath also includes any external JAR's/libraries that the code you're loading in relies upon.
However, step #2 will almost certainly not be possible on an Android device. Instead you'd need to compile Android-compatible class files somewhere else (like on a server, as Yusuf suggests), and then download and load the compiled class files from your app. Or, if you're really looking for a challenge, perhaps you could package a full Java compiler in your app, and compile the Java file(s) that way.
What you are trying to do is something similar to either the JRuby-for-Android project or AIDE both of which builds on device, but only in the case of creating apps to be run on Android as opposed to adding code functionality to an currently running app. While JRuby-for-Android is more for scripting, it may provide enough functionality as it is open source and you may be able to modify it to fit your needs.
The AIDE project appears to be more of an achievement as you can write in Java on device to build an app. Features include Dropbox and git support. AIDE appears to be a closed-source app.
Permissions will be a problem. You can't write files to the partition where the app is installed. Maybe you can if you move the app to the SD card.
An alternative is to create a custom classloader and use that to feed class files to the runtime. I don't know if you can do that on Android.
Is scripting an option? I'm sure you can find a scripting interpreter for Android. Then you can download and execute the script in the interpreter.

remote api for java

Is there a java api similar to RAPI? I want to be able to access files on the windows mobile device using a java desktop program.
Thanks.
You could use RAPI itself, and access it from Java using JNI or a wrapper like Swig
We were also looking for a similar API in Java but unfortunately none is available. I wrote my own RAPI wrapper using JNI and used that in my program.
The main problem with JNI is that any un-handled exceptions/faults cause the calling Java program to shutdown as well. Do keep that in mind when writing your wrapper. There are different approaches to safe guard these, the simplest and common approach is to write a standalone program written in .Net/C++ that communicates with the RAPI and your Java program communicates with that program using pipes/files etc. this way you also don't have to write a JNI wrapper :-)

Calling Java from Python

I have a Java app that takes a long time to be initialized (so I can't use a command-line like interface) and I need to pass text and receive the output of a Java method from Python. Is it possible to load the Java application, have it open all the time the Python script runs and use a method from that app?
I don't think the use of Python helps all that much over a command line (at least not a *nix command line), but the basic idea is to communicate over a socket or some similar mechanism. That means that the Java application will have to be wrapped in some code which opens a socket and waits for the python script to contact it. If you are most comfortable with python, you could look to implement that wrapper in Jython.
I've used JPype for something similar and it worked great.
JPype is an effort to allow python programs full access to java class libraries. This is achieved not through re-implementing Python, as Jython/JPython has done, but rather through interfacing at the native level in both Virtual Machines.
If the java app is running you should also consider xml-rpc as it also works well.
Py4J is a way to call Java from a python script. Here is the project website: http://py4j.sourceforge.net/

Categories

Resources