How to access a matlab function from java code? - java

My java program uses matlab code packaged as jar files for image processing. The problem is when I call a function(written by me) with a call to 'mmreader' for the first time, it works fine. However any subsequent call to a function(same or different) calling 'mmreader' doesn't work and I get an error stating function mmreader not found.
I am also facing a similar problem in another part of my application where the call to matlab function simply throws an exception, the same piece of code works fine in other files.
try{
vplayer.playmov(player_params);
}
catch(Exception e){
System.out.println("error playing cluster");
}

I would take a quick look at this link Accessing Matlab from Java
Here are a couple excerpts from the page that might be useful.
mlapp.MLApp mlApp = new mlapp.MLApp();
String result = mlApp.execute("a = [1 2 3 4; 5 6 7 8;]");
System.out.println("Execute result is " + result);
Inside those quotes, you can even call a MATLAB function, just make sure that you are assigning the output to the right data type.
The most important part of that link is;
We assume you have downloaded and expanded the J-Integra® kit from http://j-integra.intrinsyc.com/ and installed it correctly.
Without that installed, you cannot do the above statement.

Besides that, in MATLAB you can create a JAR with your functions and sign that JAR to only work with your applications. Perhaps you'll need the MATLAB runtime to make that app work I'm not sure about that.

Related

Calling a Java Program from Cobol

I'm trying to communicate Java and Cobol. I need to call a Java program (with paramaters) from Cobol.
I read some documentation from Microfocus:
http://supportline.microfocus.com/documentation/books/nx40/dijafc.htm
http://supportline.microfocus.com/documentation/books/nx40/dijaco.htm
But I didn't find a real solution, because I need to call an entire program and not a Java Class.
Thanks in advance.
Below is a sample program that will launch an EXE from within a COBOL97 application.
Check CallEXE demo in http://www.netcobol.com/support/code-samples/
When it comes to Microfocus...
One can not CALL an EXE from a Micro Focus INT or GNT, but you can CALL a Non-mainframe program (Micro Focus dialect in MFE) and issue a shell to DOS and from there either execute a command line that executes the EXE or execute the EXE file directly passed on the Micro Focus CALL (x'91' function code =35).
Also, you will not get back any passed parameters since once the DOS shell is closed, no parms can be returned. So the best way to get parms back is to write them to a file.
I am including a sample program that shows this x'91' FC=35 call. As you can see, you can execute a batch file or a command or an EXE directly.
Working-Storage Section.
1 Cmd-Line-Str.
2 Pic X(45)
* value 'RUN $IMSDIR\PCIMS RUNIMS BMP,DBUTIL,DEMO001T'.
* value 'run lorince'.
value 'dir c:\ /o > d.d'.
2 N-1 Pic X Value Low-Value.
1 Call-Func Pic X Comp-X Value 35.
1 Result Pic X Comp-X.
1 Cmd-Line-Len Pic X Comp-X Value 0.
Procedure Division.
P1.
Display Cmd-Line-Str upon Command-Line
Call x'91' using Result, Call-Func, Cmd-Line-Len
If Result = Zeroes
Display 'Call worked'
End-If
Goback.
I hope the post gives you some more information, I have only mainframe knowledge and haven't tried any of this above.
The link you posted explains very well about how we can instantaite a java class. If you are concerned about parameters, then write the Java Class with parameteric constructor and pass the parameters while you instantiate the Class from Cobol.
If you are confused about Java Class and Java Program, then you need to know that Java programs are compiled into .class files at the most you have executable jars containing .class files. But there is nothing like .exe for java.

Open() in Processing Code Not Working

So I asked this question concerning how to combine 3 different applications into 1 Combining Processing Applications into 1 Big Executable?
From lurking I have learned the following methods I could go about to do this:
use Java and embed my sketches in a Swing interface (rewrite
programs into some complicated Java monstrosity)
Rewitre code to launch separate windows using G4P
http://www.lagers.org.uk/g4p/ex-windows/index.html (seems better
than option 1 but still a pain in the ass because trying to write
functions to get the LEAP motion api calls to work in the separate
windows wont be easy)
Use Open() at the end of each program to run
the next program (quick and dirty, just the way I like it!)
Combine all 3 into a batch file. (I dont really understand how to go about doing this but seems similar to previous option).
Now theoretically I could get more functionality from 1 and 2 (in terms of program flow and better user interface) and would probably learn more and be better off in my future efforts, however time is of the essence at the moment. Each of the programs creates csv data files from user input that I will use to create a visualization so the manner in which I get the programs to run is somewhat irrelevant at this point. Only problem is I cant get open() to work correctly. See below:
void setup() {
size(200, 200);
}
void draw() {
// draw() must be present for mousePressed() to work
}
void mousePressed() {
open("C:/Users/corbett2/Documents/Processing/test/application.windows64/test.bat"); //doesn't work
open(new String[] { "cmd", "/c", "start", "/w", "C:/Users/corbett2/Documents/Processing/test/application.windows64/test.bat"}); //also doesn't work
}
I've tried a bunch of different ways to use open() but it wont run the program. I'm using windows 8. I exported the application "test" which created the "test.bat" located in C:/Users/corbett2/Documents/Processing/test/application.windows64. I believe you have to export your sketch before you can try to use open() on it right? As said before, the idea here is to use a call to open() at the close of each of my 3 applications in order to run them.
So my specific questions are as follows:
Which of the 4 methods I listed would you recommend I pursue for my issue? If none, please feel free to suggest something else.
Will option 3 using open() work? Why or why not?
Correct my open() issue.
Thanks in advance!
When I export application it creates test.exe for windows32 bit architecture (but running on 64bit OS) and then using your second case of open() works fine for me:
void mousePressed() {
//open second window with test app running
open("C:/Users/corbett2/Documents/Processing/test/application.windows32/test.exe");
//this will close first window
exit();
}
[but this didn't work with export to 64bit architecture (for me) so your problem should be with some settings of platform's launcher]
Anyway this approach works fine just you also need to close first window so user will not be confused.

Java interpreter code at runtime

We have discussed how java is first compiled into Java bytecode and then interpreted by the JVM. Build into the program we are using (Dr Java), there is a panel called Interactions where you can type code in real time and have it be interpreted and ran (I believe that is how it works). I was wondering if it was possible to have a compiled program in java be ran, and then allow a user to input java code to be interpreted to modify the things that happen. I can't really think of any practical uses of this, but here is an example to clarify:
User runs a program and an integer in initialized with the value of 2 and the name of changeNumber. A pop-up comes up allowing the user to input some java code. They can input something like - "changeNumber = changeNumber + 2;" and have the code execute in real time where if you ended up printing out changeNumber, you would get 4.
This is possible using the Reflection API.
As a side note, I do not understand the downvotes. This is a good and well-written question for a beginner.

java file.renameTo() does rename file but returns false. Why?

The problem is that I need the file to move before the rest of my logic will work so when the method returns false I stop execution.
However, when I check on the file in windows explorer it has a new name and it moved.
Just curious why this is happening.
here is some sample code I just tried to recreate the issue. It's pretty much the same thing and it's working fine.
File testfile = new File("TestFile");
if(!testfile.exists()){
testfile.mkdirs();
}
File sample = new File("sample.txt");
if(sample.exists()){
boolean success = sample.renameTo(new File(testfile.getPath() + "\\" + sample.getName()));
if(success){
System.out.println("Moved");
}
else{
System.out.println("Failed");
}
}
Edit: Solved it. I'm sorry for wasting everyone's time with something so silly. However, I really dont think I would have tracked this down if not for making this post.
The solution was that I was actually looping through several files to move. When the output said it failed then the program stopped and when I looked in explorer only the first of the files was actually moved so I assumed it was moving and then returning false. However, the issue was that I was using the wrong variable as an index and so what was happeneing was that it did successfully move the file in index 0 and then when the loop repeated the index didnt increment so it tried to move index 0 again and therefore failed.
Like I said, very stupid but thanks for bearing with me.
Java's File.renameTo() is problematic, especially on Windows, it seems. As the API documentation says:
Many aspects of the behavior of this method are inherently
platform-dependent: The rename operation might not be able to move a
file from one filesystem to another, it might not be atomic, and it
might not succeed if a file with the destination abstract pathname
already exists. The return value should always be checked to make sure
that the rename operation was successful.
You can use apache.commons.io library, which includes FileUtils.moveFile() or also the Files.move() method in JDK 7.
Isn't it possible that you file has a Inputstream open somewhere but has not been closed and so the rename is not working. Try closing all open streams relevant to the file object before closing.
This one worked for me
File file = new File("E:/Javadocs/" , "new.txt");
File file1 = new File("E:/Javadocs/" , "myDoc.txt");
file1.createNewFile();
if (file1.exists()){
System.out.println(file1.renameTo(file));
}
This will create a file myDoc.txt and rename it to new.txt and will print true
I've also tried with File(URI) constructor it worked fine

Dll function returns 1

Im using Java (JNA) to use a function in a third party .dll file. The functions I'm calling are returing the integer value 1.
After reading, I've discovered that this return value is traditionally 0 if everything runs correctly.
Was wondering if this is always the case or if theres any way to determine what it should be?
In the .h file bundled with the .dll it has the comment
// rc: EXIT_SUCCESS means NO ERROR
After the function.
Check actual dll documentation, there should be the way to tell what's wrong.
If nothing helps try calling GetLastError() WinAPI - some meaningful error code might be reported.
Also try to look at debug output during function call - some traces might be there even in Release build
Yes, zero typically means success in the C/C++ world.
In the days before exception handling you had to have a way to indicate failure and the return value was pretty much reserved for failure/success. As for what '1' means, you will have to look in the header of the dll for the function that is returning '1' and see if they included anything about error conditions. There are too many possibilities without seeing the code or knowing more about the dll to provide any easy answers.
What is the name of the function? What is it attempting to do? What can you do if you know the function failed?

Categories

Resources