0

I'm facing a weird problem. I'm a java beginner. I have to specificaly make a hardlink copy of one folder. This is the command if were to be used in CLI:

cp -al /home/hadoop/hdfs2/99/hdfs/* /home/hadoop/hdfs2/100/hdfs

The issue is rt.exec(command) simply fails with exit code 1. I already made the dest dir /home/hadoop/hdfs2/100/hdfs if theres any issue pops with that. However the command runs fine when ran over terminal. Also, the java runtime donot have issue executing echo "helo world" and returning inputstream.

Code:

Runtime rt = Runtime.getRuntime();
String command = "cp -al /home/hadoop/hdfs2/99/hdfs/* /home/hadoop/hdfs2/100/hdfs";
Process process;
String line;
try {

    process = rt.exec(command);
    process.waitFor();
    int exitStatus = process.exitValue();
    System.out.println(command);
    System.out.println(exitStatus);
    System.exit(1);

OUT:

cp -al /home/hadoop/hdfs2/99/hdfs/* /home/hadoop/hdfs2/100/hdfs
1
Adil Saju
  • 1,101
  • 3
  • 12
  • 26
  • 1
    Method `exec`, in class `Runtime`, is not a substitute for a terminal shell. You need to split the command into separate strings. Also consider using class [ProcessBuilder](https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html) instead of class `Runtime`. In any case, java has an API for [copying files](https://docs.oracle.com/javase/tutorial/essential/io/copy.html). – Abra Dec 25 '20 at 11:17
  • Java copy apis donot have option for hardlnk copy – Adil Saju Dec 25 '20 at 11:59
  • Since `*` is creating the issue, why not just use `cp -al /home/hadoop/hdfs2/99/hdfs/ /home/hadoop/hdfs2/100/hdfs`. Assuming it should produce the same outcome – Prasanna Dec 25 '20 at 15:09

0 Answers0