Error running code in headless mode: java.lang.NumberFormatException: For input string:

Hi all,

I have a problem with a part of my code when running on linux in headless mode. The code works as expected when run in the GUI or using the -batch option. I have tried many different things (making the number an int, running the value with &startingImage, etc.) to no luck. Below is a snippet of the code and the error message.

Thanks in advance for your help!


for (j=1; j<=iter+1;j++) {
run("Image Sequence...", "open=[filename] number=500 starting=startingImage increment=1 scale=100 file=.tif sort use");

Error Message

java.lang.NumberFormatException: For input string: "startingImage"
        at sun.misc.FloatingDecimal.readJavaFormatString(
        at sun.misc.FloatingDecimal.parseDouble(
        at java.lang.Double.parseDouble(
        at net.imagej.patcher.HeadlessGenericDialog.getMacroParameter(
        at net.imagej.patcher.HeadlessGenericDialog.addNumericField(
        at ij.plugin.FolderOpener.showDialog(
        at ij.IJ.runPlugIn(
        at ij.Executer.runCommand(
        at ij.macro.Functions.doRun(
        at ij.macro.Functions.doFunction(
        at ij.macro.Interpreter.doStatement(
        at ij.macro.Interpreter.doBlock(
        at ij.macro.Interpreter.doStatement(
        at ij.macro.Interpreter.doFor(
        at ij.macro.Interpreter.doStatement(
        at ij.macro.Interpreter.doStatements(
        at ij.plugin.Macro_Runner.runMacro(
        at ij.plugin.Macro_Runner.runMacroFile(
        at ij.IJ.runMacroFile(
        at net.imagej.legacy.IJ1Helper$
        at net.imagej.legacy.IJ1Helper$
        at net.imagej.legacy.IJ1Helper.runMacroFriendly(
        at net.imagej.legacy.IJ1Helper.runMacroFile(
        at net.imagej.legacy.LegacyCommandline$Macro.handle(
        at org.scijava.console.DefaultConsoleService.processArgs(
        at net.imagej.legacy.LegacyConsoleService.processArgs(
        at org.scijava.AbstractGateway.launch(
        at net.imagej.Main.launch(
        at net.imagej.Main.main(
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(
        at java.lang.reflect.Method.invoke(
        at net.imagej.launcher.ClassLauncher.launch(
        at net.imagej.launcher.ClassLauncher.main(

You should use string concatenation to safely generate your option string:

run("Image Sequence...", "open=[" + filename + "] number=500 starting=" + startingImage + " increment=1 scale=100 file=.tif sort use");
1 Like

Thanks @imagejan! I didn’t realize you could concatenate strings that way in .ijm macros. It works perfectly now…