我正在尝试在具有相同名称的文件上的两个文件夹上应用diff。
我正在检查文件名,然后对它们应用差异。
我也在为他们计算CKJM指标。
在运行时,它将以错误代码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]
P.S。我已经完成了几乎所有类似的问题,但看起来这有点不同。
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);
}
}
答案 0 :(得分: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并告诉它打开你的程序是什么意思。直接打开程序。
答案 1 :(得分:0)
回答这个,即使它已经老了,因为我只是在找类似的东西。 返回代码&#39; 1&#39;来自cmd.exe并指示&#34; partial&#34;根据此https://msdn.microsoft.com/en-us/library/ms194959(v=vs.100).aspx链接发布问题。它可能没有达到运行&#34; diff&#34;一点都不或者,如上所述,cmd.exe无法找到要传递给diff的文件。同样,这会导致diff实际上没有被调用,返回代码来自shell;不是差异程序。 HTH