import java.io.*;
import java.util.*;
class copyData
{
public static void main(String[] args){
try{
Runtime r = Runtime.getRuntime();
System.out.println("Hello World!");
Process p = r.exec("copy txn.txt xyz1.txt");
System.out.println("process: "+p);
} catch(Exception e){
e.printStackTrace();
}
}
}
this code is displaying some exception @ runtime:
java.io.IOException: CreateProcess: copy txn.txt xyz1.txt error=2
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at copyData.main(copyData.java:9)
can any one help me.......
Thanks for ur replies.....
ahh.. my mistake.. sorry jwenting and thanks for pointing it out
It should be:
String winCmd = "xcopy .......";
Runtime.exec( winCmd );
Also, I guess Satyasai.praveen is using Windows platform (because he/she mentioned C/D drive). As for you, there is no xcopy command under Linux, you may need to change that to whatever command that suits you.
> import java.io.*;
> import java.util.*;
> class copyData
> {
> public static void main(String[] args){
> try{
> Runtime r = Runtime.getRuntime();
> System.out.println("Hello World!");
> Process p = r.exec("copy txn.txt xyz1.txt");
> System.out.println("process: "+p);
> } catch(Exception e){
> e.printStackTrace();
> }
> }
> }
>
>
> this code is displaying some exception @ runtime:
>
>
> java.io.IOException: CreateProcess: copy txn.txt
> xyz1.txt error=2
> at java.lang.ProcessImpl.create(Native
> Method)
> at java.lang.ProcessImpl.<init>(Unknown
> Source)
> at java.lang.ProcessImpl.start(Unknown
> Source)
> at java.lang.ProcessBuilder.start(Unknown
> Source)
>at java.lang.Runtime.exec(Unknown Source)
> at java.lang.Runtime.exec(Unknown Source)
>at java.lang.Runtime.exec(Unknown Source)
> at copyData.main(copyData.java:9)
>
> can any one help me.......
>
> Thanks for ur replies.....
Hmm... read this:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4086045
> > Hmm... read this:
> >
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=408
>
> > 6045
>
> Why? That wasn't related to the problem that the OP
> has. The problem is that copy/xcopy must be executed
> within a shell, so the OP has to use cmd /c xcopy.
>
> Kaj
You are right Kaj, i did a quick search and posted it with the thought that it was the solution to it.
> ahh.. my mistake.. sorry jwenting and thanks for
> pointing it out
>
> It should be:
>
> String winCmd = "xcopy .......";
> Runtime.exec( winCmd );
>
No, it shouldn't.
You assume that everyone is using a computer with a specific operating system and fail therefore to envision situations in which that operating system (and its associated commands) isn't available.
In this rather benign case all that would happen is the program failing to work, but the result could be disastrous if there were a command which just happens to have the same name and accepts the same parameters but does something completely different and potentially destructive.
jwenting... I do agree with what you said. If you read my first post in this thread, I was suggest this "simplest" solution to the PO, with knowing he/she is using windows platform.
Runtime.exec() is a command which can be disasterous if you do not know what you are doing. I understand that even if you have the same command name on different platforn that do totally different things could be very dangerous, this can even happen on the same platform. e.g. someone could rename format.exe command to xcopy.exe.
If you want to know assumption for the simple code I have written, well:
1) Windows platform
2) Original Windows xcopy command with the right parameters
3) Programmer who knows what this code will do
feel free to add on more assumptions if you think this is important.
If you have better solution, I believe PO is very interested to know, so do I. I can think of many other ways to achieve it, pure java codes, but I was just provide PO with the simplest solution.
> You can do everything using File, maybe some
> IOStreams, and a little recursion.
I think that's debateable.
I would advise the OP to use a software tool to copy the contents of the C: drive to the D: drive. I'd also advise them to give us the high level view of their requirements; perhaps they're trying to clone a bootable drive? Just back up part of the system? Something else?
Depending upon what they're doing Java may or may not be part of the solution, but by default assuming that just getting a workable image of the filesystem is their target, Java is not a suitable hammer for this particular screw.
> > You can do everything using File, maybe some
> > IOStreams, and a little recursion.
>
> I think that's debateable.
>
I didn't mean you (or OP) personally, and of course everything as in the requirement to copy all files from one drive to another and not everything anyone could ever want to do in any way ;)
> I didn't mean you (or OP) personally, and of course
> everything as in the requirement to copy all files
> from one drive to another and not everything anyone
> could ever want to do in any way ;)
Even that though. In the OP's case I think you'd have some problems copying some system files.