2015-01-15 72 views
0

我正在构建一个工具,它应该通过Windows命令提示符从格式A转换为格式B.我编程了一些像交互模式的东西。 cmd等待输入命令并处理它们。 现在我想通过一些参数,如果我打电话给程序。参数将被传递,但程序不会自行执行命令。自动执行args程序

我传递ARGS这样的:

java -jar test.jar -interactive //activates the interactive mode, which does not make any problems

和这样的(通过源文件,在转换过程中所使用的目标的地方保存转换后的文件,目标格式和最后配置文件):

java -jar test.jar C:\Users\User\Desktop\test.json C:\Users\User\Desktop .xes C:\Users\User\Desktop\test.properties

代码到目前为止:

public static void main(final String[] args) throws IOException, InterruptedException { 

    if (args[0].equals("-interactive")) { 
     testing test = new testing(); 
     test.startCMD(); 
    } else if (args[0] != null && args[1] != null && args[2] != null && args[3] != null) { 
     final testing test = new testing(); 
     test.startCMD(); 
     //the following construct doesn't work unfortunatelly 
     worker = Executors.newSingleThreadScheduledExecutor(); 
     Runnable task = new Runnable() { 

      @Override 
      public void run() { 
        test.setSourceFile(args[0]); 
        test.setTargetPath(args[1]); 
        test.setTargetFormat(args[2]); 
        test.setConfigFilePath(args[3]); 
        System.out.println("Invoking the conversion routine now ..."); 
        ConverterControl control = new ConverterControl(); 
        control.link(sourceFilePath, targetPath, configFilePath, targetFormat, fileConfig); 
      } 

     }; 
     worker.schedule(task, 5, TimeUnit.SECONDS); 
    } 
} 

public void startCMD() throws IOException, InterruptedException { 
    String[] command 
      = { 
       "cmd",}; 
    Process p = Runtime.getRuntime().exec(command); 
    new Thread(new SyncPipe(p.getErrorStream(), System.err)).start(); 
    new Thread(new SyncPipe(p.getInputStream(), System.out)).start(); 
    BufferedReader in = new BufferedReader(
      new InputStreamReader(System.in)); 
    System.out.println("Welcome to the Windows command line prompt." + System.lineSeparator()); 
    System.out.println("Please type \"help\" to receive a list of commands available in this environment."); 
    //String s = in.readLine(); 
    String input; 
    while ((input = in.readLine()) != null) { 
     //process the inputs 
} 

您在main方法中看到的不同setter只是将传递的信息设置为声明在类顶部的变量。之后这些变量应该被传递给ConverterControl,它在其link()方法中具有整个转换例程。 执行startCMD()命令后,如果我传递4个参数(请参阅顶部程序的第二个调用),程序将停止。

有谁知道如何调用这些方法并自动启动ConverterControl

回答

1

您可能会陷入startCmd()末尾的while循环中,并且您的程序永远不会继续。

0

如果您收到命令行参数,为什么要调用startCMD()?

另一方面,它看起来像你也可能滥用静态变量一点。

为了整理这些,您的'测试'类应该实现Runnable,并且您应该将对'test'对象的引用传递给您的工作者。

if (args[0].equals("-interactive")) { 
    testing test = new testing(); 
    test.startCMD(); 
} else if (args[0] != null && args[1] != null && args[2] != null && args[3] != null) { 
       final testing test = new testing(); 
     test.startCMD();//Why is this call here? 

     test.setSourceFile(args[0]); 
     test.setTargetPath(args[1]); 
     test.setTargetFormat(args[2]); 
     test.setConfigFilePath(args[3]); 

     worker = Executors.newSingleThreadScheduledExecutor(); 
     worker.schedule(test, 5, TimeUnit.SECONDS); 
} 

run()方法的其余部分将被简单地移出到'testing'类的run()方法中。

@Override 
     public void run() { 
       ConverterControl control = new ConverterControl(); 
       control.link(sourceFilePath, targetPath, configFilePath, targetFormat, fileConfig); 
     }