java中的cleartool命令执行 - runtime()。exec

时间:2014-04-16 14:43:13

标签: java jenkins-plugins runtime.exec cleartool

尝试实现jenkins插件,该插件应使用cleartool命令VOB在指定的cleartool lstpye -kind lbtype -s -inv \VOB下显示基线,但在命令执行过程中获取错误(VOB是输入参数)。不知道问题究竟在哪里,或者我在代码中做错了什么。我将非常感谢您的建议。

这是代码

public class MakeViewDefinition extends SimpleParameterDefinition{

private String vob;

public static String getVob(String vob) throws IOException{
    try{        
        String[] cmd = {"cmd.exe", "/C", "cleartool", "lstype", "-kind", "lbtype", "-s", "-inv", "\\", vob};
        Process p=Runtime.getRuntime().exec(cmd);
        BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        String output = "";
        while((line=reader.readLine()) != null){
            output +=line+ "\n";    
        }
        return output;
    }
    catch ( IOException ioe )  
    {  
        System.out.println("ERROR: " + ioe); 
    }
    return "WARNING: End of getVob()\n"; 
}

@DataBoundConstructor
public MakeViewDefinition(String name, String description, String vob){
    super(name, description);
    this.vob= vob;
}

protected static List<String> getList (String output){

    return getList(output);
}

 @Extension
 static public class DescriptorImpl extends ParameterDescriptor{
     @Override
     public String getDisplayName(){
         return Messages.MakeViewDefinition_DisplayName();
     }

     public FormValidation doCheckVob(@QueryParameter String vob){
         if (StringUtils.isBlank(vob)){
             return FormValidation.error(Messages.MakeViewDefinition_vob_empty());
         }
     return FormValidation.ok();
     }

     public FormValidation doTest(@QueryParameter String output) throws IOException{
         List<String> filelist = getList(getVob(output));
         if(filelist.isEmpty()){
             return FormValidation.ok("(No File matched)");
         }
         return FormValidation.ok(StringUtils.join(filelist, '\n'));
     }
 } 

@Override
public ParameterValue createValue(String value) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
    // TODO Auto-generated method stub
    return null;
}
}

config.jelly

<f:entry title="${%Name}" field="name">
    <f:textbox />
</f:entry>
<f:entry title="${%Description}" field="description">
    <f:textarea />
</f:entry>
<f:entry title="${%VoB Directory}" field="vob">
    <f:textbox />
</f:entry>
<f:validateButton
    method="test"
    title="${%List Files Now}"
    progress="${%Checking...}"
    with="vob"
/>

index.jelly

<f:entry title="${it.name}" description="${it.description}">
    <div name="parameter" description="${it.description}">
        <input type="hidden" name="name" value="${it.name}" />
        <j:scope>
            <j:set var="instance" value="${it.defaultParameterValue}" />
            <myF:staticSelect
                name="value"
                field="value"
                vob="${it.vob}"
            />
        </j:scope>
    </div>
</f:entry>

以下是错误消息:

Caused by: java.lang.NullPointerException
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at de.bosch.MakeView.MakeViewDefinition.getVob(MakeViewDefinition.java:34)
at de.bosch.MakeView.MakeViewDefinition$DescriptorImpl.doTest(MakeViewDefinition.java:80)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.kohsuke.stapler.Function$InstanceFunction.invoke(Function.java:298)
at org.kohsuke.stapler.Function.bindAndInvoke(Function.java:161)
at org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:96)
at org.kohsuke.stapler.MetaClass$1.doDispatch(MetaClass.java:120)
at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:53)
at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:728)
... 69 more

1 个答案:

答案 0 :(得分:1)

首先,您需要确保已装入vob(如果您使用的是动态视图),并且您的命令应使用"\\"+vob,而不是"\\", vob

作为mentioned here,NPE表示其中一个参数为null,如果输入错误的名称就是这种情况(例如&#39; xxx&#39;而不是{{1 }}。
如果是这种情况,您可以跳过&#39; \xxx&#39;在\\数组中。