Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

copy with command prompt

  Asked By: Brayden    Date: Feb 04    Category: Java    Views: 6258

How can I write commands in command prompt(cmd) with java.
In detail I want to copy the selected file with FileChooser from it's
directory to the destination that specified from filename. but this
copy command is possible only with cmd.
my problem is that I don't know how to call cmd and so how to command
in it;



5 Answers Found

Answer #1    Answered By: Shirley Allen     Answered On: Feb 04

you can build dynamic command  as String and Run it by Invoking :
Runtime.getRuntime().exec(String command);

Answer #2    Answered By: Myrna Brown     Answered On: Feb 04

I faced this problem  some time ago, but the solution Mr.HaghighatKhah recommended (and also using class "ProcessBuilder") does not work for all the commands  executed in cmd environment.
for instance consider the command  "copy d:\music\someFile.txt d:\downloads"
executed in cmd environment correctly, but when running the following code you'll notified the occurrence of this exception : "Exception in thread "main" java.io.IOException: Cannot run program "copy": CreateProcess error=2, The system cannot find the file  specified"


String cmd = "copy d:\\music\\someFile.txt d:\\downloads";
try {
} catch (IOException e) {


Answer #3    Answered By: Reginald Thomas     Answered On: Feb 04

You can write  a batch (.bat on Windows OS) or shell script (.sh on Linux OS) file
which performs the exact copy.
for example in Windows you can have "copy.bat" with this contents:

copy "%1" "%2"

and then you can call  

exec("PATH_TO_COPY_BAT/copy.bat d:/music/someFile.txt d:/downloads");

Alternatively you can do it with java. Read the contents of the file  with BufferedReader and write it to a newly created file with BufferedWriter. Use buffer to improve the performance:

bytes buffer = new byte[BUFFER_SIZE];
while (reader.read(buffer) != -1)

Answer #4    Answered By: Seth Anderson     Answered On: Feb 04

This is because "exec" looks for a program named "copy" and tries to execute it.

But there is no "copy.exe", (you can probably use xcopy.exe instead!).

Copy is one of the shell commands  that the shell handles itself.

For running shell commands, you should run "cmd" with /C param, and pass your command  .

For example, you should pass "cmd /C copy  d:\\music\\someFile.txt d:\\downloads" as the parameter for the exec method

Answer #5    Answered By: Jeanne Lawson     Answered On: Feb 04

the point is that there is no copy.cmd or copy.exe executable files on any windows box!
the commands  like:
cd, cls, date, del, dir, echo, erase, path, prompt, rd etc.
are only interpreted by the executable command.com. however there should be a xcopy.exe on your windows box under the path C:\WINDOWS\system32. so try to run the program with the command: "xcopy sourceFile destinationFile" and then it should work.
however xcopy asks a question:

Does C:\\dst.txt specify a file  name
or directory name on the target
(F = file, D = directory)?

which we should handle as well (before running the code you should of course create a src.txt file on C:\\ directoy):

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.OutputStream;

public class ProcessTest {

public static void main(String[] args) throws Exception {

Process process = Runtime.getRuntime().exec("xcopy /y c:\\src.txt c:\\dst.txt");

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

System.out.println("xcopy says: " + reader.readLine() + " " + reader.readLine());

// and we answer...

OutputStream os = process.getOutputStream();




Didn't find what you were looking for? Find more on copy with command prompt Or get search suggestion and latest updates.