Create directory in Javascript

In regular macro programming, you can create a directory with


I tried in Javascript


But I get

Error: Java class "" has no public instance field or method named "makeDirectory". 

Use File.mkdir().

Error: Java class “” has no public instance field or method named “mkdir”.

Still getting an error.

You need to create a new File object pointing at the desired directory path, and then call the mkdir method on it to create it on disk. See this article on object oriented JavaScript for an introduction to these concepts, if you aren’t familiar with them.

I’m familiar with object oriented Javascript. But this has to do with the way Java libraries are exposed in ImageJ, or maybe it’s just Rhino. This stuff isn’t terribly well documented.

var fileObject = new File(folder);
directoryName = folder + "testDirectory";
print("making directory " + directoryName);
Error: Can't find method (<Unknown source>#19) in <Unknown source> at line number 19

The output from print(fileObject.listFiles()) is telling me something about what is going wrong, but I’m not sure what.

You’re directly interacting with the Java classes here, so the Javadoc for ImageJ1 and (in the case of Java itself should help you. Please feel free to add to the Javascript scripting page whatever you think is missing.

To your specific case:


There’s no such package. I wonder why you didn’t get an error message here. Use importClass(; instead.

Error: Can't find method

Again, look at the javadoc of the mkdir() method doesn’t take any arguments. The directory name has to be provided in the contructor of the File object.

var pathToNewDirectory = "/path/to/folder/testDirectory";
var fileObject = new File(pathToNewDirectory);

Knowing the macro command File.makeDirectory, you could have found this in

1 Like

OK. I’m starting to see how this works. I’ll use that as a reference.

1 Like