I am trying to apply diff on two folders on the files which have same name.
I am checking name of files and then applying diff over them.
I am also calculating CKJM Metrics for them.
On running it is exiting with error code 1.
Kindly help in running CMD operations through java program .
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. I have gone through nearly all similar questions asked here but it seems like This is a bit different.
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);
}
}
Consider using
ProcessBuilder
instead of Runtime.exec(), it allows you a lot more control than Runtime.exec(), and will allow you to read the stdout and stderr of the utility you are calling, which will help you find the cause of your problem.Another thing is you pass your files to your utility using
File.getName()
which is only going to return the name of the file itself, not its complete path. Therefore, yourdiff
utility will interpret those file names according to its working directory, which is probably different from the folder where your files are stored. To solve this, you can either useProcessBuilder
to set the working directory before invoking your utility, or you can useFile.getAbsolutePath()
orFile.getCanonicalPath()
to pass the full path.I'm not sure if it is relevant on Windows, but have you tried removing the trailing spaces from the arguments you pass to
Runtime.exec()
? Remember that you are not actually building a command string. The arguments that you pass toRuntime.exec()
will be passed "as-is" to your operating system, and will end up "as-is" in the created process. in your case, this means that your OS will be looking for a binary called "cmd.exe ", which I'm pretty sure doesn't exist. Same goes for all your other parameters.Also, Don't call "cmd.exe", call your program directly. What's the point of calling the shell and telling it to open your program. Just open the program directly.