-1

我正在尝试对具有相同名称的文件的两个文件夹应用差异。

我正在检查文件的名称,然后对它们应用差异。

我也在为他们计算 CKJM Metrics。

运行时它以错误代码 1 退出。

请帮助通过 java 程序运行 CMD 操作。

Error : Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
Exited with error code 1
The common file names are [Inverse Trigono.doc, Limit _ Continuity & Differentia
bility.doc, Parabola.doc, Permutation and combination.doc, Probability.doc, Quad
ratic Equation and Expression.doc, Sequence and Series.doc, Solution of triangle
.doc, Straight Line.doc, TEST PAPER.rar, Vectors.doc]

ps 我已经解决了几乎所有类似的问题,但似乎这有点不同。

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ListFiles111 {

    public static void main(String[] args) {

            // Path of Folder 1
        String path1 = "C:\\Users\\hi\\Downloads\\IIT Typing\\IIT Typing"; 

            // Path of Folder 2
        String path2 = "C:\\Users\\hi\\Downloads\\IIT Typing\\IIT Typing"; 2

        File folder1 = new File(path1);
        File folder2 = new File(path2);

        ArrayList<String> commonfiles = new ArrayList<>();

            // Array list of files of folder 1 created.
        List<File> filesList1 = Arrays.asList(folder1.listFiles());

            // Array list of files of folder 1 created
        List<File> filesList2 = Arrays.asList(folder2.listFiles());


        for (File f1 : filesList1) 
        {
            if (f1.isFile()) 
            {
                for (File f2 : filesList2) 
                {
                    if (f2.isFile() && f1.getName().equals(f2.getName())) 
                    {
                                      // Adding common name files in new Array list
                        commonfiles.add(f1.getName());                                          
                        try 
                        {
                            Runtime rt = Runtime.getRuntime();

                            String[] cmd = new String[5];
                            cmd[0] = "cmd.exe " ;
                            cmd[1] = "/C " ;
                            cmd[2] = "diff ";
                            cmd[3] = f1.getName() + " ";
                            cmd[4] = f2.getName();

                            Process pr = rt.exec( cmd );

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

                            String line = null;

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

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

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

                        try 
                        {
                            Runtime rt = Runtime.getRuntime();

                            String[] cmdd = new String[6];
                            cmdd[0] = "cmd.exe " ;
                            cmdd[1] = "/C " ;
                            cmdd[2] = "java ";
                            cmdd[3] = "-jar ";
                            cmdd[4] = "C:\\Users\\hi\\Desktop\\ckjm-1.9\\build\\ckjm-1.9.jar ";
                    cmdd[5] = "C:\\Users\\hi\\Desktop\\*.class";

                            Process pr = rt.exec( cmdd );

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

                            String line = null;

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

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

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

                    }
                }
            }
        }

        System.out.println("The common file names are " + commonfiles);
    }
}
4

2 回答 2

0

考虑使用ProcessBuilder而不是 Runtime.exec(),它允许您比 Runtime.exec() 进行更多的控制,并允许您读取正在调用的实用程序的 stdout 和 stderr,这将帮助您找到原因问题。

另一件事是您将文件传递给您的实用程序,使用File.getName()它只会返回文件本身的名称,而不是其完整路径。因此,您的diff实用程序将根据其工作目录解释这些文件名,这可能与存储文件的文件夹不同。要解决此问题,您可以ProcessBuilder在调用实用程序之前使用设置工作目录,也可以使用File.getAbsolutePath()File.getCanonicalPath()传递完整路径。

我不确定它是否与 Windows 相关,但您是否尝试过从传递给的参数中删除尾随空格Runtime.exec()?请记住,您实际上并没有构建命令字符串。您传递给的参数Runtime.exec()将“按原样”传递给您的操作系统,并在创建的进程中“按原样”结束。在您的情况下,这意味着您的操作系统将寻找一个名为“cmd.exe”的二进制文件,我很确定它不存在。所有其他参数也是如此。

另外,不要调用“cmd.exe”,直接调用你的程序。调用 shell 并告诉它打开你的程序有什么意义。直接打开程序就行了。

于 2013-12-03T09:21:21.533 回答
0

回答这个问题,即使它很旧,因为我只是在寻找类似的东西。返回代码“1”来自 cmd.exe,根据此https://msdn.microsoft.com/en-us/library/ms194959(v=vs.100).aspx链接表示存在“部分”问题。它可能根本没有运行“差异”。或者,正如上面所建议的,cmd.exe 找不到要作为输入/输出传递给 diff 的文件。同样,这将导致 diff 实际上没有被调用,并且返回码来自 shell;不是差异程序。Hth

于 2017-08-29T07:36:13.200 回答