How do I use user defined Java classes within Matlab? - java

I have read the documentation and several websites on exactly how to do this, however Matlab does not seem to pick up the classes that I have added to the dynamic java class path. Nor do I use the right syntax to correctly construct the object.
I have an class HandDB and which to create an object of this type and invoke it's static methods to connect to a SQL database. The class has an empty constructor and takes no parameters. The class is part of a package 'nuffielddb' which I made in a project within Netbeans. All the files are on my usb stick which is my E:\ drive...
I would like to be able to use all the classes within the package. The package is contained at E:\nuffielddb.
I entered the following commands into Matlab:
javaaddpath('E:\');
javaclasspath; % Output from java class path includes E:\ within dynamic path
str = java.lang.String('Test'); % Works fine
db = nuffieldbd.HandDB(); % Does not work - undefined variable or class error
Interesting I typed 'import nuffielddb.*;' and received no error.
Just where am I going wrong?
Thanks for your help btw!

Ah problem solved! Well not solved in a sense! I found out it's actually a problem with my matlab installation and I have no idea how to fix it :-(
Never mind, it works on the computers at the office :-)

if your classes are in a .jar file, make sure your classpath includes the .jar file name itself (not just the directory it's in).
Also if the MATLAB JRE is Java 1.5 (R2006b is, whereas R2009a is Java 1.6, not sure when they switched), make sure your classes are compiled with 1.5 as a target, not 1.6, otherwise MATLAB will not be able to use them.

Minor note: .* imports will never error, so they're not diagnostic. They simply add a package to the list that Matlab searches through when trying to resolve a class name. Nonexistent packages are ignored.
>> import this.package.does.not.exist.*
>>

Related

Call a Java class that is in a different Java file than the calling Java class

So, I have a Java file called MinTest.java which contains a call to a class called Min which is located in another file called Min.java. Therefore, the layout is: The MinTest.java method calls the Min class that is in min.java.
I am compiling the code using "javac MinTest.java" but it does not work because it does not recognize Min.
How do I deal with this? I do not want to use an IDE, I need to compile and run everything from the command line. I am using ubuntu.
Thank you.
class Min shall be in Min.java, and to use it in another class Min.class shall be on compilation classpath ( and of course, on execution classpath) - consult javac documentation how to setup it. But to be hones, you can as well do this in IDE. Almost nobody does compile manually. Use ant/maven/gradle/IIDE ofyour choice

NDK build in command line to rebuild my application

My project use native library. when I change the package name , I got following error.
code:
Native method not found:
com.nooshindroid.yastashir.controller.JNIServer.get_number_of_processors:()I
at com.nooshindroid.yastashir.controller.JNIServer.get_number_of_processors(Native Method)
at com.nooshindroid.yastashir.controller.JNIServer.runServer(JNIServer.java:27)
at com.nooshindroid.yastashir.game.FreebloksActivity.startNewGame(FreebloksActivity.java:520)
at com.nooshindroid.yastashir.game.FreebloksActivity$16.onClick(FreebloksActivity.java:774)
at com.nooshindroid.yastashir.game.ColorListDialog.onItemClick(ColorListDialog.java:79)
when I sreach the net I understand that I must rebuil my JNI folder in command line.
In command line I don't know how to give project path.
I read somewhere that I must write like this .
cd <project>
$ <ndk>/ndk-build
I don't know the steps to do that job.
it show my project path and when I write Dir , it shows me all of things that exist in my folder.
but nothing change.
WHen you use JNI, unless you specify otherwise, the package name is part of the function name it looks for in the C code. So if you change the package name in Java, you also need to change all your C code JNI function names.
As #GabeSechan has said, you need to edit the C or C++ code to match the new package name. When you change the package name, the directory structure changes. If you don't edit the C or C++ code to match, the methods won't be found.
You need to change your JNI methods from
Java_com_old_package_name_methodName
to
Java_com_new_package_name_methodName
See the JNI documentation for an example.
Rebuild your native code after renaming the methods.

Compiling Error Java

So i have been working on my Project1 and For some reason i cant figure out why It wont run. I get the Error "Could not find the main class". What am i doing wrong?
My code is:
https://gist.github.com/anonymous/6604f427cc9d17391478
I'm not sure how to post all the code properly with out making it super confusing (I tried to figure it out earlier) But let me know if i can help!
Is there something wrong with my code? Or do i need to compile it in a certain way?
if your using eclipse, goto to run configurations, select:
Project: my Project1
Main Class: assignment1.Assignment1_test
this will work for sure :)
Let's say you have a folder/package assignment1 somewhere on your file system inside which you have your Assignment1_test and Fraction class.Refer the screenshot above to compile and run your code. :)
You have it in a package named assignment1. This means it is in a folder named assignment1. After compiling, go up to the folder that contains assignment1 then run java assignment1.Assignment1_test from there.
java expects a fully-qualified class name (the name of the class including the package). It also expects that the class is in your classpath (. is implicitly added). Packages are tied directly to the directory structure.
Combining that all together, since the full-qualified name assignment1.Assigment1_test must be specified to java, and since the package structure is the directory structure, then the class is expected to be in assignment1\ relative to the current directory and therefore you must be in the directory that contains assignment1 to execute it (unless it is somewhere else in your classpath, which, given your situation, I'm guessing is not the case).

Matlab doesn't see .jar file

Ok, I'm stumped here. I'm using Matlab version 2013b with a Java RTE of 1.7.0_11 and I'm trying to run a simple piece of code to see if Matlab is able to read the .jar file and nothing seems to be working.
Here is the Java code, which is compiled to a .jar named JavaOCT.jar, which is placed in the Matlab working directory:
package VTK;
public class vtkVolumeView{
public int Test(){
return 10;
}
}
That is it, no other dependencies, nothing fancy. In Matlab, I try:
javaaddpath('\JavaOCT.jar'); %<-Directory and name are 100% correct
import VTK.*; %<-Package name from above
methodsview VTK.vtkVolumeView; %<-Can't find the class, argh!
Matlab kicks back that it can't find the class.
Things I've done to try and solve the problem:
Reverted to the exact same JDK as the Matlab RTE
Tried an older 1.6 JDK
Done lots of stack overflow research to try and solve it 1 2 3 4
Tried used javaclasspath and pointing to the compiled class instead
Read the Matlab documentation 5
Using clear -java after the javaaddpath
Any help would be appreciated, it is driving me nuts!
Update: Daniel R suggested just javaaddpath('JavaOCT.jar') which doesn't work either.
Final update: It finally works! I wasn't building the .jar properly. In IntelliJ, click on the project and hit F4. This brings up the Project Structure, then go to Artifacts and click the green + button and add DirectoryContent and then point to the out\production. Once this is done, as mentioned by others, it should show up in Matlab as an expandable .jar.
I don't know which operating system you are using, but the ./ seems invalid.
Try javaaddpath('JavaOCT.jar'); or javaaddpath(fullfile(pwd,'JavaOCT.jar'));.
What does exist(fullfile(pwd,'JavaOCT.jar')) return?
Some things to try:
Add the class file. When using a package, you need to add the class file in at the host of the package. For example, if your code is here:
\\full\path\to\code\VTK\vtkVolumeView.class
Then use:
javaaddpath('\\full\path\to\code')
I'm still suspicious of your *.jar path. You should usually use absolute paths when adding jar files. Try adding the results of which('JavaOCT.jar')
How did you make your jar file? Does it contain the appropriate directory structure implied by your package declaration?

JPype Headaches

I've found several instructions on how to import user-built .class and .jar files to JPype, but I seem to be having a lot of trouble getting anything working at all.
What works: I can import standard java stuff and print HELLO WORLD and such.
Some of what I've tried:
I've tried adding -Djava.class.path with the path to a jar containing the relevant class files, to a directory structure containing (several folders down) the relevant .class files, as well as '-Djava.ext.dirs'. I've recompiled and re-installed with a different JVM location. The class I am attempting to instantiate is Outer, public, and has a public constructor.
I'm using Python 2.6.1 on OSX 10.6.
My current test file:
from jpype import *
startJVM(getDefaultJVMPath(), '-Djava.class.path=/Users/gestalt/Documents/msmexplorer_git/msmexplorer/MSMExplorer/build/classes')
java.lang.System.out.println("hello world")
msmexplorer = JPackage('org.joofee.meh.msmexplorer')
T = msmexplorer.MSMExplorer()
shutdownJVM()
If I use JClass I always get ClassNotFound exceptions from JPype; if I use JPackage I get Package not callable errors. Basically, JPype can't find my stuff.
Thanks so much!
EDIT (possibly helpful debugging stuff...):
Is there a straightforward way to print which third party java classes are available/imported?
Package not callable errors are referenced in this link) it would seem you need to make sure the java class file is accessible from the working directory. I am not sure how the jvm classpath comes into play, I would have thought how you did it would work.
You could also try loading the org package and then getting to the other packages through that one as the link I shared shows:
msmexplorer = JPackage('org').joofee.meh.msmexplorer
T = msmexplorer.MSMExplorer()

Categories

Resources