Using Protocol Buffer Java Bindings in Matlab - java

I am trying to use Java protobuf stubs inside Matlab. I generated the Java stub and corresponding jar file in Eclipse. I then take the jar file and add it to the Matlab path. In Matlab I do the following:
import raven.aos.*;
import raven.aos.Messages.*;
image = Image.newBuilder();
At this point I get an error message that says:
??? Undefined variable "Image" or class "Image.newBuilder".
Error in ==> pub>pub.pub at 16
image = Image.newBuilder();
I have successfully been able to use the Java jar in a Java project using the exact same syntax. So this validates that my stub is correct. I have also successfully imported and used a different Java library, zmq.jar, in my Matlab project, so to a certain extent this verifies that I know how to import jar files properly into Matlab.
I've refrained from attaching the generated Java stub file since it is very long. I hope that someone can point out what I'm doing wrong with just the code that I've provided. If required, I will add the stub source.
Thanks in advance!

Because generated protocol buffer message classes are inner classes, you need to use Matlab's javaMethod command to get to the static methods. Import statement will not work. Using your example:
image = javaMethod('newBuilder','raven.aos.Messages$Image');
http://www.mathworks.com/help/techdoc/ref/javamethod.html

Related

Eclipse not able to see javax package

I'm currently trying to fiddle with images, specifically convert images from JPEG, WEBP, and BMP forms to PNG forms and my method uses the javax.imageio.ImageIO class. When I tried importing it, Eclipse yelled that the package that the type was not accessible. I thought that was weird and went digging through StackOverflow on my own and found multiple answers saying I should remove and re-add the JRE. This didn't work, somewhat unsurprisingly, but while looking through my build path I noticed that the JRE was missing the entire javax package. Is there a reason this could be? Is there a fix?
The exact error reads The type javax.imageio.ImageIO is not accessible and the suggested edits ask me if I want to make class ImageIO in package javax.imageio.
I am using the latest build of Eclipse. My JDK is java-16-openjdk-amd64. I am running Ubuntu 20.04. I built this app from the ground up, so I am not using Maven (unless Eclipse uses Maven by default).
I tried compiling a basic class in my command line and it worked for some reason, despite not working in Eclipse.
I would rather not revert my JDK to an older version if I don't have to.
It turns out I was being just being an idiot. It turns out I had actually made this with a module without realizing it. All it took for me was to get rid of the module file.
You do not call "new" on a static class
To make an instance non static of it if it ever does have such a type available from one of its static methods you cast it to that type.
However, with the javax.imageio.ImageIO you make other classes from its methods.
import java.awt.image.BufferedImage;
import java.io.*;
try{ // wrap in FileNotFoundException IOException
File input = new File("/somewhere/over/the/rainbow/cementplant.jpg");
//static classes are called directly with a method
BufferedImage bfi = (BufferedImage)javax.imageio.ImageIO.read(input);

Parsing xml in Matlab (using JAXB)

I'm trying to parse a xml-file based on a xsd in Matlab.
As recommended in this thread MATLAB parse and store XML with XSD, I'm trying to use JAXB to do this, but I got stuck.
I was able to create the .java class files representing my xsd-levels via
xjc myFile.xsd –d myDirectory (within the windows command-line).
I've read the linked articles in the other thread, but now I don't know how to go on.
What are the next steps before I can go on in Matlab?
Thanks in advance
Edit:
Matlab Version is 2010b (Java 1.6.0_17)
Edit2:
I tried now JAXB.unmarshal(input, MyClass.class) as I managed to get the MyClass.class by using an eclipse JAXB-Project (only using the command line input mentioned above just gave me the MyClass.java files).
But Matlab can't find the method JAXB.unmarshal(probably because of the java version of matlab?).
After adding the folder, where my package is in(D:\PathToMyPackage\my\Package\MyClass.class), to the dynamic java path in Matlab
javaaddpath('D:\PathToMyPackage') and after importing 'my.package' I was also trying:
jc = JAXBContext.newInstance('my.package')
jc = JAXBContext.newInstance('ObjectFactory.class')
jc = JAXBContext.newInstance('my.package.ObjectFactory.class')
But each time I get a JAXB Exception eg.
javax.xml.bind.JAXBException: "my.package" doesnt contain
ObjectFactory.class or jaxb.index
although they're inside the folder.
So is it even possible to perform the whole JAXB-thing only in Matlab? Or do I first have to wrap around some classes in eclipse to export the whole thing as a .jar-file, which I then import in Matlab?
If it is possible, which mistakes could I make? Or what could have an impact?
Newer Java version in eclipse, the dynamic java path in Matlab?
If you use Java 8 and don't need xml validation, converting .xml source into Java object is quite easy:
MyClass myObject = JAXB.unmarshal(input, MyClass.class);
Here, input is either InputStream, File, URL etc, pointing to .xml source, MyClass is a class that represents root element--one which has #XmlRootElement annotation. It can be found among classes that were generated by xjc.
See more:
https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/JAXB.html

Integrating a Java Class into a PyDev Project (Jython) in Eclipse

I have a Java class that i would like to import into my Jython script. Unfortunately, eclipse won't let me create a Java class inside my Jython project.In the window, where you create and name your Java class, I get a message at the top (alongside a red cross) saying, "Source folder is not a Java project" when I type the name of the would be class. How do I rectify this? I need the Java Class to call C code using the JNI (declaring the native method,loading and then calling it). Thank You !!!!!!!
What you can do is to create second module which would be java project. Anyhow, logically it should be that way. Please check out other similar question - PyDev: Jython modules & Java classes in the same project.
Other links that might help - http://pydev.org/manual_101_project_conf2.html
So what nefo_x suggested is correct. You need to create a new Java project that will contain your Java class. Then import the Java package as you would a python module. But there are a few things to watch out for in eclipse to make it work. I list the whole process below:
Your Java class (or classes) should not be in the default package. You need to create a new package and make/put your java class files there.
Export the package as a jar file to some place on your computer.
Add the jar file (located at some place on your computer) to your python path.
Import the package by writing "import PackageName".
The problem for me was that I had my java class in the default package. That does not work due to some naming issues. Anyhow, hope that this helps.

Java Classes from WSDL and Eclipse

I am trying to create and use a set of Java classes from WSDL using Apache Axis 2. I’ve successfully generated the class files using the approach described here:
http://www.gerd-riesselmann.net/scala/creating-java-classes-wsdl-file-using-apache-axis-2
I have copied the generated class files into my eclipse development environment. I have obtained all the necessary jar files and I have added them to my environment.
A single (source) problem remains that I don’t understand. The code section looks like this:
...
public org.tempuri.PreprocessingIncidentImportServiceStub.ImportResponse
import (org.tempuri.PreprocessingIncidentImportServiceStub.Import import0)
throws java.rmi.RemoteException
{
org.apache.axis2.context.MessageContext _messageContext = null;
...
Eclipse is reporting: Syntax error on token "import", expected identifier.
I don't understand the function definition that includes the "import" statement, and I am wondering if there's some eclipse setting that I must use to resolve this problem.
Thanks in advance for any help.
It seems that the method is named import. Import is a reserved word in Java so you can't name a method with it.
Assume that you have imported the classes and doesn't need to use the full qualified name of them. This is what you have:
public ImportResponse import (Import import0) throws RemoteException{
//...
}

No class located error in Matlab while calling java class

I have developed an Matlab code for stegnography which requires some preprocessing to be done by a Java file.
The java file aes1.java contains the function encrypt(String s) which needs to be called by matlab code:
javaaddpath('C:\Users\Aneesh\Desktop\BE_Project');
disp(char(javaMethod('hash', 'aes1', userText)));
I am getting the following error:
Error using javaMethod
No class aes1 can be located on the Java class path
Error in project_mod (line 11)
disp(char(javaMethod('hash','aes1',userText)));"
Versions I'm using:
Matlab 2013a
Java 1.6
Any help would be greatly appreciated!
Update:
I have a .jar file which contains all the classes i need. I've imported it and tried using it but I'm still getting the same error!
Please take a look at the documentation. javaaddpath adds one or more folders of java Archive (jar) files.
You need to compile your java file to a class-file and add it using javaclasspath

Categories