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

标签 java jenkins-plugins runtime.exec cleartool

尝试实现一个jenkins插件,该插件应使用cleartool命令cleartool lstpye -kind lbtype -s -inv\VOB显示指定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;
}
}

配置.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"
/>

索引.果冻

<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

最佳答案

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

mentioned here ,NPE 表示参数之一为 null,如果您输入错误的名称(例如“xxx”而不是 \xxx),就会出现这种情况。
如果是这种情况,您可以跳过 cmd 数组中的“\\”。

关于java - java中的cleartool命令执行-runtime().exec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23112741/

相关文章:

java - 在选项卡更改后(使用支持库),先前分离的 fragment 在当前 fragment 后面保持可见

java - 如何从 spring-dispatcher-servlet.xml 访问 SessionFactory 到 DAO

java - parseDouble 与 nextDouble

jenkins - 如何使用 Jenkins http-request 插件和管道在正文中发布 JSON 数据?

Jenkins Naginator 插件和正则表达式

java - 来自java Runtime.exec的ffmpeg水印找不到输出

java - Tomcat无法访问java API创建的文件

Java:从 HTTP 服务器上的套接字读取 POST 数据

linux - 如何在 Jenkins 中设置管道构建的字符串名称

java - 如何使类可用于包外的运行时 java 命令