Run Windows Command in Jenkins Plugin

161 views
Skip to first unread message

micha...@gmail.com

unread,
Jul 19, 2016, 2:52:59 AM7/19/16
to Jenkins Developers
Hi,

I would like to run a Windows command inside my Jenkins Plugin.

How can I achieve that?

The following code is not working:
@Override
public void perform(Run<?,?> build, FilePath workspace, Launcher launcher, TaskListener listener) {
try {
Launcher.ProcStarter procStarter=launcher.launch().pwd(workspace).cmdAsSingleString("dir " + workspace + " &&  attrib -R /S");
procStarter= procStarter.stdout(listener);
procStarter.join();
 } catch (IOException e) {
e.printStackTrace();
listener.getLogger().println("IOException !");
} catch (InterruptedException e) {
e.printStackTrace();
listener.getLogger().println("InterruptedException!");
}

}

I get the following error when I run a Job that uses this plugin:
java.io.IOException: Cannot run program "dir" (in directory "C:\LocalJenkins\plu
gins\michaelPlugin\work\jobs\buildTest\workspace"): CreateProcess error=2, The s
ystem cannot find the file specified

        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
        at hudson.Proc$LocalProc.<init>(Proc.java:244)
        at hudson.Proc$LocalProc.<init>(Proc.java:216)
        at hudson.Launcher$LocalLauncher.launch(Launcher.java:816)
        at hudson.Launcher$ProcStarter.start(Launcher.java:382)
        at hudson.Launcher$ProcStarter.join(Launcher.java:389)
        at michaelPlugin.michaelPlugin.HelloWorldBuilder.perform(HelloWorldBuild
er.java:110)
        at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibili
tyLayer.java:75)
        at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
        at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBui
ld.java:785)
        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.j
ava:537)
        at hudson.model.Run.execute(Run.java:1741)
        at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
        at hudson.model.ResourceController.execute(ResourceController.java:98)
        at hudson.model.Executor.run(Executor.java:408)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find th
e file specified
        at java.lang.ProcessImpl.create(Native Method)
        at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
        at java.lang.ProcessImpl.start(ProcessImpl.java:137)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
        ... 16 more
Jul 19, 2016 8:51:12 AM hudson.model.Run execute
INFO: buildTest #52 main build action completed: SUCCESS

What should be the correct code to run some Windows commands?

Many thanks

Mads Nielsen

unread,
Jul 19, 2016, 6:15:42 AM7/19/16
to jenkin...@googlegroups.com
I think you need to prepend cmd.exe to the dir command so something like cmd.exe /C dir


____________________________________________________________
Mads Nielsen
Consultant
m...@praqma.net
+45 50 98 18 09
Skype: inkspot
Praqma
www.praqma.com
DK: CPH, Aarhus, Allerod
NO: OSL
SE: STHLM
+45 36 PRAQMA

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/3d3bcaef-9079-4d56-96a7-f09719865a7d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Baptiste Mathus

unread,
Jul 19, 2016, 7:49:57 AM7/19/16
to Jenkins Developers

BTW, it's not a Jenkins specific question. More like a general Java one. So googling should give many answers.

Cheers


Reply all
Reply to author
Forward
0 new messages