1

In my company we are updating web software that uses a Perl script to perform specific operations. In updating the script, it will be rewritten in Java.

The main operations of the script are to move between folders and copy / remove files within them.

I need to perform Java commands like chdir, rcopy, rmove, fcopy, etc which are used in the current Perl script. Searching both on the web and here on Stack Overflow I read that Java does not offer equivalent commands. Is there a way to perform these operations?

For now from the Java documentation I was thinking of using java.nio.file.Files. Are there better alternatives?

halfer
  • 19,824
  • 17
  • 99
  • 186
64Bit1990
  • 302
  • 2
  • 16
  • why can't you use absolute path instead? – pedrohreis Nov 24 '21 at 10:08
  • I don't use absolute path because the path has to be dynamic depending on the data passed to the script. For this reason I have saved the paths in a configuration file with `Properties scriptProperties = new Properties ();` and depending on the operation I take the properties necessary to create the path and perform the operation – 64Bit1990 Nov 24 '21 at 10:11
  • I don't think this question should be closed - the other question does not have answers, nor does it have alternatives. – TheKodeToad Nov 24 '21 at 10:18
  • 1
    you can convert from relative path to absolute with `getAbsolutePath` – pedrohreis Nov 24 '21 at 10:19
  • 1
    @MrParrot The duplicate target already asks the same question. And it has answers. The fact that the answers say "it's not possible" is irrelevant. If anyone knows a better answer, then the place to post it is on the duplicate target. – Dada Nov 24 '21 at 10:29

2 Answers2

1

If you can use absolute paths (you can convert a relative path to an absolute path by means of getAbsolutePath), it should be as simple as:

Path source = Paths.get("/my/source/dir/sourcefile.txt");
Path target = Paths.get("/my/target/dir/targetfile.txt"); 

Files.move(source, target); // move
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); //copy
pedrohreis
  • 1,030
  • 2
  • 14
  • 33
  • If you want to use paths from the configuration, you can do `Paths.get(dooDah.getProperty("filePath"), "relativefile.txt")` – TheKodeToad Nov 24 '21 at 10:20
1

I'm not sure if you can execute all of those commands in Java (because I'm not familiar with all of them), but at least for the usual System commands (like "chdir") should be possible using this approach:

Process exec = Runtime.getRuntime().exec("sudo -v");

or:

Process exec = Runtime.getRuntime().exec("pkill -f " + processName);

or like this:

String[] cmd = { "/bin/sh", "-c", "cd /var; ls -l" };
Process p = Runtime.getRuntime().exec(cmd);

As you can see "exec" method requires a string containing your desired console command. Also for more complex explanations you can consult this book: https://alvinalexander.com/java/edu/pj/pj010016/. I hope I gor it right ( your question I mean). Cheers!

Vladmix6
  • 9
  • 2