0

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.

SteewDK
  • 363
  • 1
  • 4
  • 14
  • 1
    It is throwing an exception, isn't it? What's its stacktrace (use `e.printStackTrace()` instead of `System.out.println("Some Error")`)? – watery Jan 06 '14 at 16:44
  • @watery, yes it is throwing an exception. Let me get back tou you on that. – SteewDK Jan 06 '14 at 16:45
  • what exception? my crystal ball says that matlab is running as a different user and does not have the right permissions. – jtahlborn Jan 06 '14 at 16:47
  • @jtahlborn How can I tell which exception it throws? I have just (for simplicity) created the try/catch with Exception. – SteewDK Jan 06 '14 at 16:48
  • 1
    See my *edited* comment. It may be worth printing the current directory, just to be sure you're creating the folders where you expect them. – watery Jan 06 '14 at 16:50
  • @watery see my update. I would have to ask you what you mean by printing the current directory? Is it not exactly what mkdirs() does? Please correct me if I am wrong. – SteewDK Jan 06 '14 at 16:52
  • You path is relative. It is likely that the current directory for Matlab is not the same current directory used when you run the java test – cmd Jan 06 '14 at 16:53
  • You could do something like `pwd` or `dir` no? `mkdirs()` makes the directory (I think. Please correct me if I'm wrong). – DLJ Jan 06 '14 at 16:55
  • @cmd the JAVA files and MATLAB file is not in the same directory, that is correct. But should'nt it simply just create the directory in the current directory of the MATLAB file or am I wrong in this? – SteewDK Jan 06 '14 at 16:55
  • @SteewDK try appending [pwd](http://www.mathworks.com/help/matlab/ref/pwd.html) to the front of the dir path – turbo Jan 06 '14 at 16:57
  • @SteewDK yes mkdirs() will create the dir hierarchy assuming you have permission. Does mkdirs() throw an exception. does mkdirs throw a SecurityException (note its a runtimeexception)? – cmd Jan 06 '14 at 16:59
  • @cmd The try/catch statement terminates by `dir.mkdirs();`. I do not know how to figure out what type of exception that is being thrown. I should have permission though - because similar pieces of code have worked before. – SteewDK Jan 06 '14 at 17:02
  • @turbo, I am not certain it would make sense to append pwd to the front of the dir path. The reason: My code is written in java. This includes the path of directory. The only thing I do in Matlab is calling the method inside my .jar file. – SteewDK Jan 06 '14 at 17:06
  • wait a second, the path you are making is `Patients/Patient#/Parameters`, but the error says the path `Patients/Patient#/Meals/meal0.csv` is not found. Are you using a different path in the code you omitted that hasn't been made? – turbo Jan 06 '14 at 17:12
  • @turbo good point, it is simply because I have two statements that does exactly the same thing. The only difference is the name - one for the meals and one for parameters. The problem still exist though. – SteewDK Jan 06 '14 at 17:15
  • Ah, ok, just making sure that wasn't overlooked, sometimes it's the simple things. – turbo Jan 06 '14 at 17:18
  • @turbo, you are right. But thanks! – SteewDK Jan 06 '14 at 17:18

2 Answers2

0

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.

Lucas
  • 2,514
  • 4
  • 27
  • 37
  • Thanks for your reply. I am in doubt whether it is a good idea to use an absolute path since my program should be used across platforms on many different computers. – SteewDK Jan 06 '14 at 17:17
  • @SteewDK You should be able to get the current directory somehow (I'm not familiar with the Java I/O API) and prepend that to your path. – watery Jan 06 '14 at 17:50
0

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").
Community
  • 1
  • 1
stav
  • 1,497
  • 2
  • 15
  • 40