Hi all,
In my Java program I am launching batch script(.bat file).
I am facing following error:

 FATAL: command execution failed
    java.io.IOException: Cannot run program "cmd" (in directory "C:\..\Project_Name"): CreateProcess error=87, The parameter is incorrect
        at java.lang.ProcessBuilder.start(Unknown Source)
        at hudson.Proc$LocalProc.<init>(Proc.java:244)
        at hudson.Proc$LocalProc.<init>(Proc.java:216)
        at hudson.Launcher$LocalLauncher.launch(Launcher.java:815)
        at hudson.Launcher$ProcStarter.start(Launcher.java:381)
        at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:1148)
        at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:1113)
        at hudson.remoting.UserRequest.perform(UserRequest.java:120)
        at hudson.remoting.UserRequest.perform(UserRequest.java:48)
        at hudson.remoting.Request$2.run(Request.java:326)
        at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:68)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at hudson.remoting.Engine$1$1.run(Engine.java:62)
        at java.lang.Thread.run(Unknown Source)
        at ......remote call to VM1(Native Method)
        at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1416)
        at hudson.remoting.UserResponse.retrieve(UserRequest.java:220)
        at hudson.remoting.Channel.call(Channel.java:781)
        at hudson.Launcher$RemoteLauncher.launch(Launcher.java:928)
        at hudson.Launcher$ProcStarter.start(Launcher.java:381)
        at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:95)
        at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:64)
        at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
        at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:782)
        at hudson.model.Build$BuildExecution.build(Build.java:205)
        at hudson.model.Build$BuildExecution.doRun(Build.java:162)
        at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534)
        at hudson.model.Run.execute(Run.java:1738)
        at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
        at hudson.model.ResourceController.execute(ResourceController.java:98)
        at hudson.model.Executor.run(Executor.java:410)
    Caused by: java.io.IOException: CreateProcess error=87, The parameter is incorrect
        at java.lang.ProcessImpl.create(Native Method)
        at java.lang.ProcessImpl.<init>(Unknown Source)
        at java.lang.ProcessImpl.start(Unknown Source)
        at java.lang.ProcessBuilder.start(Unknown Source)
        at hudson.Proc$LocalProc.<init>(Proc.java:244)
        at hudson.Proc$LocalProc.<init>(Proc.java:216)
        at hudson.Launcher$LocalLauncher.launch(Launcher.java:815)
        at hudson.Launcher$ProcStarter.start(Launcher.java:381)
        at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:1148)
        at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:1113)
        at hudson.remoting.UserRequest.perform(UserRequest.java:120)
        at hudson.remoting.UserRequest.perform(UserRequest.java:48)
        at hudson.remoting.Request$2.run(Request.java:326)
        at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:68)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at hudson.remoting.Engine$1$1.run(Engine.java:62)
        at java.lang.Thread.run(Unknown Source)
    Build step 'Execute Windows batch command' marked build as failure
    Finished: FAILURE

I have made Jenkins service as Windows service and tried with providing administrator privileges to the service. But I am facing the same error.

Following are the ways I am launching my batch script:

Desktop.getDesktop().open(new File("C:\\file1.bat"));
and
Runtime.getRuntime().exec("cmd /c C:\\file1.bat");

Please anyone tell me how to solve this(In this case my all client machines are windows machines).

Dani AI

Generated

the stacktrace shows Jenkins failing to start the command interpreter itself, not your .bat. On Windows, CreateProcess error 87 means ERROR_INVALID_PARAMETER, which usually comes from an invalid command line or working directory being passed to CreateProcess. Running the Jenkins agent as a service or as admin will not fix that by itself. Also, avoid Desktop.getDesktop() here; services typically run without an interactive desktop and Desktop may throw a HeadlessException. See the Java docs for details. (learn.microsoft.com)

is on the right track about using ProcessBuilder and passing args separately. Make the interpreter explicit, quote only the .bat path, and set the working directory so Windows is not left to guess. Example:

String bat = "C:\\path\\to\\file1.bat";  // quote needed if path has spaces
ProcessBuilder pb = new ProcessBuilder(
    "C:\\Windows\\System32\\cmd.exe", "/c", "\"" + bat + "\""
);
pb.directory(new File("C:\\path\\to\\Project_Name"));
pb.redirectErrorStream(true);
Process p = pb.start();

This avoids ambiguous PATH lookups of "cmd" and handles spaces correctly. ProcessBuilder is designed for this style of argument passing. (docs.oracle.com)

Quick checks that often resolve error 87 in Jenkins on Windows:

  • Verify the shell Jenkins will use: add a one-line build step to print echo %COMSPEC% and where cmd. %COMSPEC% should point to C:\Windows\System32\cmd.exe. If it does not, fix the environment for the service account. (learn.microsoft.com)
  • If you can, let Jenkins run the batch directly instead of spawning from Java: use the Freestyle "Execute Windows batch command" step or the Pipeline bat step. Jenkins runs these via cmd.exe /c for you. (jenkins.io)

If the above still fails, double-check that the working directory exists and the .bat path is valid and properly quoted. Even a small mismatch there can surface as error 87. (learn.microsoft.com)

  1. Use ProcessBuilder, which is the more recent replacement for Runtime exec
  2. Parse the args yourself and pass then separately to ProcessBuilder, because its default parsing of command and parameters is a frequent source of problems. eg try something like

new ProcessBuilder("cmd", "/c", "C:\file1.bat").start();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.