0

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:

  1. Reverted to the exact same JDK as the Matlab RTE
  2. Tried an older 1.6 JDK
  3. Done lots of stack overflow research to try and solve it 1 2 3 4
  4. Tried used javaclasspath and pointing to the compiled class instead
  5. Read the Matlab documentation 5
  6. 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.

Community
  • 1
  • 1
Austin
  • 1,018
  • 9
  • 20
  • You need to add the full path and it should NOT be in the MATLAB's directory. – Oleg Dec 18 '13 at 18:15
  • It is, the `javaclasspath` shows the exact location, including the .jar extension of the JavaOCT.jar file using either Daniel R's method or the original method above. There is no warning error as is seen when a bad path is given to `javaaddpath` – Austin Dec 18 '13 at 18:23
  • Can you post the output of javaclasspath, just the relevant lines with etc.? – Oleg Dec 18 '13 at 18:33
  • Sure. This is from a fresh start of Matlab. I switch to the directory of the .jar file and type `javaaddpath('\JavaOCT.jar')` and then type `javaclasspath` and get `DYNAMIC JAVA PATH C:\Users\Austin\Desktop\MatlabOCT\trunk\JavaOCT.jar` – Austin Dec 18 '13 at 18:45
  • 1
    What's the directory structure of the jar file? (you can simply expand it like an directory in the matlab "explorer"). Did you build it yourself using `javac` or with an IDE, such as Eclipse? See e.g. http://stackoverflow.com/q/9974882/2319400 for what it should look like. – sebastian Dec 18 '13 at 20:45
  • 1
    Thank you! I was using IntelliJ to build the .jar and wasn't doing it properly. In IntelliJ, one has to manually add the output directory to the .jar. After building the .jar properly, things work using the method above. If you answer it, I'll accept. – Austin Dec 19 '13 at 04:23

2 Answers2

1

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?

Daniel
  • 36,610
  • 3
  • 36
  • 69
  • Hey Daniel, I apologize, it is just '\JavaOCT.jar', question has been edited to show this. – Austin Dec 18 '13 at 16:51
  • Afaik, `\JavaOCT.jar` is an **absolute** path for all os supported by matlab. Remove the ` \ ` – Daniel Dec 18 '13 at 16:56
  • I dunno, it doesn't show an error, and when I type `javaclasspath` the file is properly shown in the `DYNAMIC JAVA PATH`. Let me try and I'll update the question – Austin Dec 18 '13 at 16:59
1

Some things to try:

  1. 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')
    
  2. 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')

  3. How did you make your jar file? Does it contain the appropriate directory structure implied by your package declaration?

Pursuit
  • 12,285
  • 1
  • 25
  • 41
  • As to question 1, zomg it works, the methods are displayed! As to question 2, it shows the correct .jar file. Question 3: I just used the default IntelliJ settings for a .jar artifact. Perhaps this is the issue with the .jar file? – Austin Dec 18 '13 at 18:55