Question

I am out of luck with my JAVA Application executed from MATLAB. In short I have following code:

try {
        dir = new File("Patients/Patient" + patientNumber + "/Meals");  
        dir.mkdirs(); 
        .... more code goes here
} catch (Exception e) {
        System.out.println("Some Error");   

For some reason, and I do not know why, this code runs perfectly when executed from the JAVA main method. However, from MATLAB this piece of code does not work. It terminates at dir.mkdirs() and hence never creates the directory. I had success using mkdirs() and mkdir() many times before, so, I suspect the problem exist in MATLAB. Do you have any idea what is the reason?

Stack Trace:

e.printStackTrace();

Returns following:

java.io.FileNotFoundException: Patients\Patient1\Meals\meal0.csv (The system cannot find the path specified.)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at inputOutput.PrintPatientCsv.printMealCsv(PrintPatientCsv.java:57)
at inputOutput.CreatePopulation.createpopulation(CreatePopulation.java:54)
at functionality.FactoryModel.loadData(FactoryModel.java:100)

The last three lines is a product of the directory not created. However, I am not certain of the meaning of the top 4 lines in above code.

Program details

My Java program is created inside a .jar file. This .jar file is located in:
C:\Users\myName\program\binJava

my matlab (where I execute the .jar file from) file is located in:
C:\Users\myName\program\matlab

In my MATLAB I type following:

clear all
javaaddpath('..\binJava\myFile.jar')
import functionality.*;
import domain.*;
import test.*;
import inputOutput.*;
function.MyFunction(1,2,3);

The reason for the import statements is that my program is build into 4 different packages.

*UPDATE: * I just found out that the program works if I create absolute directory as sugested by lnunno. This still does not solve the problem though.

Was it helpful?

Solution

I don't have the rep required to comment, but have you tried using an absolute path?

java.io.FileNotFoundException: Patients\Patient1\Meals\meal0.csv (The system cannot find the path specified.)

Means that java is unable to find the file that you are specifying with your relative path. I'm not sure how this changes when you call it from Matlab, but maybe you can specify where this directory lives with an absolute path and test that out and report back with the stack trace.

OTHER TIPS

MATLAB and the Java Virtual Machine (JVM) it hosts are two different beasts running concurrently under the same process. As such, they have different notions of the current working directory. Specifically, MATLAB's current working directory starts from the default directory it displays when loading and will follow any cd commands you do using the GUI or command line. The JVM's current working directory is the one from which the java binary was started from. Since MATLAB does this from it's default directory, that directory is what the JVM will consider current and this will never change.

In MATLAB to get the working directory use pwd. In Java (under a JavaSE JVM) use System.getProperty("user.dir").

Unfortunately, in your case, since you are hoping to use a path relative to where your Java code is, neither of the above will be of use.

Solutions:

  1. Go a little hard core and dig out the directory of your JAR file from your Java code so you can prepend it to your path like so.
  2. If you can count on the current MATLAB folder to be a good reference for your needs pass in the value returned from cwd into your Java functions.
  3. If MATLAB's default path is relevant, you can assume your Java code will have that directory as the current one which you can get by calling System.getProperty("user.dir").
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top