2

我一直在尝试运行

C:\Users\Luis\Desktop\RESEARCHGUIPROJECT\ResearchGUI\CHEMKED\CHEMKED_SOLVER.exe

使用 java 或让 java 使用命令提示符运行此可执行文件。现在我相信我能够将 java 获取到文件但是当它运行它时,可执行文件给我ERR IO-21 invalid STATUS specifier for given file

我已经从 DOS 窗口运行了可执行文件,通常当给出该代码时,这意味着输入文件的指定不正确。为了提供求解器的背景知识,它的作用是从名为solverfilepath.txt的文件中读取,并在该文件中为文件所在的位置提供一个文件目录。该文件是名为SOLTMP.txt的求解器输入。

此错误仅在我运行 java 时发生,但在我从命令窗口手动运行时不会发生。

我不知道当 java 运行这个程序时是否有办法让它打开命令窗口以查看在命令提示符下运行的可执行文件。

有什么建议么?

    int i = 1;
    while(finalTime < 1.0000){

        try{

        // Execute CHEMKED solver

        // Added 07/06/2013 from StackOverFlow
        Runtime rt = Runtime.getRuntime();
        String[] CHEMKED = {"C:\\Users\\Luis\\Desktop\\RESEARCHGUIPROJECT\\ResearchGUI\\CHEMKED\\CHEMKED_SOLVER.exe"};
        Process pr = Runtime.getRuntime().exec(CHEMKED); 

        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

        String line=null;

        while((line=input.readLine()) != null) {
            System.out.println("*****");
            System.out.println(line);
        }

        int exitVal;
            exitVal = pr.waitFor();
        System.out.println("Exited with error code "+exitVal);

        } catch(Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
        }

        try{
            inputFile3 = new Scanner(new FileReader("CHEMKED\\SOLTMP.txt"));
        }catch(Exception e){
            e.printStackTrace(System.out);
        }

        try{
            copyFile = new Formatter(new File("CHEMKED\\output-ring1_"+ i +".txt"));
        }catch(Exception ex){
            ex.printStackTrace(System.out);
        }

        //copy SOLTMP content to temporary file
        while(inputFile3.hasNextLine()){
            fileLine = inputFile3.nextLine();
            copyFile.format("%s%n",fileLine);
        }

        copyFile.close();
        inputFile3.close();

         // Added 07/05/2013
         initialTime+= 0.10;
         finalTime+= 0.10;

         // Added 07.03
         updateFile();

    i++;      
    }

所以这是我到目前为止添加的内容,与在 javaWorld 上所做的类似。我还没有机会运行它,但只是看看我是否朝着正确的方向前进。

class StreamGobbler extends Thread
{
InputStream is;
String type;
OutputStream os;

StreamGobbler(InputStream is, String type)
{
    this(is, type, null);
}
StreamGobbler(InputStream is, String type, OutputStream redirect)
{
    this.is = is;
    this.type = type;
    this.os = redirect;
}

public void run()
{
    try
    {
        PrintWriter pw = null;
        if (os != null)
            pw = new PrintWriter(os);

        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line=null;
        while ( (line = br.readLine()) != null)
        {
            if (pw != null)
                pw.println(line);
            System.out.println(type + ">" + line);    
        }
        if (pw != null)
            pw.flush();
    } catch (IOException ioe)
        {
        ioe.printStackTrace();  
        }
    }
}

public void chemked(String args[])  {

    int i = 1;
    while(finalTime < 1.0000)   {
    if (args.length < 1)
    {
        System.out.println("USAGE java GoodWinRedirect <outputfile>");
        System.exit(1);
    }
        try{

        FileOutputStream fos = new FileOutputStream(args[0]);

        String[] CHEMKED = { "C:\\Users\\Luis\\Desktop\\RESEARCHGUIPROJECT\\ResearchGUI\\CHEMKED\\CHEMKED_SOLVER.exe"}; 

        Process pr = Runtime.getRuntime().exec(CHEMKED); 

        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

        // any error message
        StreamGobbler errorGobbler = new 
            StreamGobbler(pr.getErrorStream(), "ERROR");            



        // any output
        StreamGobbler outputGobbler = new 
            StreamGobbler(pr.getInputStream(), "OUTPUT", fos);

        // start them off
        errorGobbler.start();
        outputGobbler.start();

        String line=null;

        while((line=input.readLine()) != null) {
            System.out.println("*****");
            System.out.println(line);
        }

        int exitVal;
            exitVal = pr.waitFor();
        System.out.println("Exited with error code "+exitVal);
        fos.flush();
        fos.close();

        } catch(Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
        }

        try{
            inputFile3 = new Scanner(new FileReader("CHEMKED\\SOLTMP.txt"));
        }catch(Exception e){
            e.printStackTrace(System.out);
        }

        try{
            copyFile = new Formatter(new File("CHEMKED\\output-ring1_"+ i +".txt"));
        }catch(Exception ex){
            ex.printStackTrace(System.out);
        }

        //copy SOLTMP content to temporary file
        while(inputFile3.hasNextLine()){
            fileLine = inputFile3.nextLine();
            copyFile.format("%s%n",fileLine);
        }

        copyFile.close();
        inputFile3.close();

         // Added 07/05/2013
         initialTime+= 0.10;
         finalTime+= 0.10;

         // Added 07.03
         updateFile();

    i++;      
    }
  }

让我知道谢谢。

解决方案:

    int i = 1;
    while(finalTime < 1.0000)   {

        try{

        FileOutputStream fos = new FileOutputStream("C:\\Users\\Luis\\Desktop\\RESEARCHGUIPROJECT\\ResearchGUI\\CHEMKED\\error" +i+".txt");

        String[] CHEMKED = { "C:\\Users\\Luis\\Desktop\\RESEARCHGUIPROJECT\\ResearchGUI\\CHEMKED\\CHEMKED_SOLVER.exe", "SOLTMP.txt"}; 

        //Process pr = Runtime.getRuntime().exec(CHEMKED); 

        ProcessBuilder builder = new ProcessBuilder(CHEMKED);

        builder.directory(new File("C:\\Users\\Luis\\Desktop\\RESEARCHGUIPROJECT\\ResearchGUI\\CHEMKED"));

        builder.redirectError();

        Process pr =   builder.start();

        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

        // any error message?
        StreamGobbler errorGobbler = new 
            StreamGobbler(pr.getErrorStream(), "ERROR");            

        // any output?
        StreamGobbler outputGobbler = new 
           StreamGobbler(pr.getInputStream(), "OUTPUT", fos);

        // kick them off
        errorGobbler.start();
        outputGobbler.start();

        int exitVal;
            exitVal = pr.waitFor();
        System.out.println("Exited with error code "+exitVal);
        fos.flush();
        fos.close();

        } catch(Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
        }

        try{
            inputFile3 = new Scanner(new FileReader("CHEMKED\\SOLTMP.txt"));
        }catch(Exception e){
            e.printStackTrace(System.out);
        }

        try{
            copyFile = new Formatter(new File("CHEMKED\\output-ring1_"+ i +".txt"));
        }catch(Exception ex){
            ex.printStackTrace(System.out);
        }

        //copy SOLTMP content to temporary file
        while(inputFile3.hasNextLine()){
            fileLine = inputFile3.nextLine();
            copyFile.format("%s%n",fileLine);
        }

        copyFile.close();
        inputFile3.close();

         // Added 07/05/2013
         initialTime+= 0.10;
         finalTime+= 0.10;

         // Added 07.03
         updateFile();
    i++;      
    }
}
4

1 回答 1

1

同样,考虑使用 ProcessBuilder 并将其工作目录设置为与您的执行文件相同的位置。就像是:

  String path = "C:/Users/Luis/Desktop/RESEARCHGUIPROJECT/ResearchGUI/CHEMKED/";
  String app = "CHEMKED_SOLVER.exe";
  List<String> command = new ArrayList<String>();
  // command.add(path);
  command.add(app);
  ProcessBuilder processBuilder = new ProcessBuilder(command);
  processBuilder.directory(new File(path));
  processBuilder.redirectError();
  Process process = processBuilder.start();

  BufferedReader bReader = new BufferedReader(
      new InputStreamReader(process.getInputStream()));
  String line = "";
  while ((line = bReader.readLine()) != null) {
     System.out.println(line);
  }
于 2013-07-07T03:35:20.717 回答