Java 差异正在退出,错误代码为1
我正在尝试对同名文件上的两个文件夹应用diff 我正在检查文件名,然后对它们应用diff 我还在为他们计算CKJM指标 运行时,它正在退出,错误代码为1 请通过java程序帮助运行CMD操作Java 差异正在退出,错误代码为1,java,exec,metric,Java,Exec,Metric,我正在尝试对同名文件上的两个文件夹应用diff 我正在检查文件名,然后对它们应用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 cod
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、 我已经看过了这里几乎所有类似的问题,但这似乎有点不同
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);
}
}
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
公共类列表文件111{
公共静态void main(字符串[]args){
//文件夹1的路径
String path1=“C:\\Users\\hi\\Downloads\\IIT Typing\\IIT Typing”;
//文件夹2的路径
String path2=“C:\\Users\\hi\\Downloads\\IIT Typing\\IIT Typing”;2
File folder1=新文件(路径1);
File folder2=新文件(路径2);
ArrayList commonfiles=新建ArrayList();
//已创建文件夹1的文件数组列表。
List filelist1=Arrays.asList(folder1.listFiles());
//已创建文件夹1的文件数组列表
List filelist2=Arrays.asList(folder2.listFiles());
对于(文件f1:filesList1)
{
if(f1.isFile())
{
对于(文件f2:filelist2)
{
如果(f2.isFile()&&f1.getName().equals(f2.getName()))
{
//在新数组列表中添加公用名称文件
commonfiles.add(f1.getName());
尝试
{
Runtime rt=Runtime.getRuntime();
字符串[]cmd=新字符串[5];
cmd[0]=“cmd.exe”;
cmd[1]=“/C”;
cmd[2]=“diff”;
cmd[3]=f1.getName()+“”;
cmd[4]=f2.getName();
进程pr=rt.exec(cmd);
BufferedReader输入=新BufferedReader(
新的InputStreamReader(pr.getInputStream());
字符串行=null;
而((line=input.readLine())!=null)
{
系统输出打印项次(行);
}
int exitVal=pr.waitFor();
System.out.println(“已退出,错误代码为”
+exitVal);
}
捕获(例外e)
{
System.out.println(例如toString());
e、 printStackTrace();
}
尝试
{
Runtime rt=Runtime.getRuntime();
字符串[]cmdd=新字符串[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”;
进程pr=rt.exec(cmdd);
BufferedReader输入=新BufferedReader(
新的InputStreamReader(pr.getInputStream());
字符串行=null;
而((line=input.readLine())!=null)
{
系统输出打印项次(行);
}
int exitVal=pr.waitFor();
System.out.println(“已退出,错误代码为”
+exitVal);
}
捕获(例外e)
{
System.out.println(例如toString());
e、 printStackTrace();
}
}
}
}
}
System.out.println(“公共文件名为“+公共文件”);
}
}
考虑使用ProcessBuilder
而不是Runtime.exec(),它允许您比Runtime.exec()进行更多的控制,并允许您读取正在调用的实用程序的stdout和stderr,这将帮助您找到问题的原因
另一件事是使用File.getName()
将文件传递给实用程序,该实用程序只返回文件本身的名称,而不返回其完整路径。因此,您的diff
实用程序将根据其工作目录解释这些文件名,该目录可能与存储文件的文件夹不同。要解决此问题,可以在调用实用程序之前使用ProcessBuilder
设置工作目录,也可以使用File.getAbsolutePath()
或File.getCanonicalPath()
传递完整路径
我不确定它是否与Windows相关,但您是否尝试过从传递给Runtime.exec()的参数中删除尾随空格?请记住,您实际上并不是在构建命令字符串。传递给Runtime.exec()
的参数将“按原样”传递给操作系统,并在创建的进程中“按原样”结束。在您的情况下,这意味着您的操作系统将寻找一个名为“cmd.exe”的二进制文件,我很确定它不存在。所有其他参数也是如此
另外,不要调用“cmd.exe”,直接调用您的程序