This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes using windows command line to copy and paste. getting file not found error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using windows command line to copy and paste. getting file not found error" Watch "using windows command line to copy and paste. getting file not found error" New topic
Author

using windows command line to copy and paste. getting file not found error

john bullwinkle
Greenhorn

Joined: Nov 04, 2010
Posts: 4
i'm needing to copy and paste + rename a file from one dir to another. i tried doing it in strictly java code, i was successful, but when opening the file, it is corrupt. so i decided to switch my method to just pass a copy line to the windows command line. i have successfully ran the copy line in the windows command prompt, however am getting an error in my code when doing it. here is the code



here is the output i'm getting

copy = COPY "C:\serviceTech\config\Tech\LPEquipmentLease_distributed.pdf" "C:\serviceTech\CompletedForms\ItemLPEqpLsetest_test_test.pdf"
java.io.IOException: Cannot run program "COPY": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at servicetech.SelectionScreen.getFileNames(SelectionScreen.java:338)

if i copy everything after the 'copy =' line into the command line, it works fine. so i'm unsure why i'm getting this cannot find the file specified.

thanks in advance for your help.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Welcome to the Ranch, John!

COPY is not an application; it's a built-in command of CMD.EXE. You'll need to run that using the /C flag. Run CMD /? for more info.

But I'd first investigate why the files became corrupted. Can you show us the old Java copy code?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
john bullwinkle
Greenhorn

Joined: Nov 04, 2010
Posts: 4
thanks for the help. not sure if i fully understand the way i should format the line.

i tried this:


it runs, however doesn't copy the file.

here is the original code i used to copy. option1-3 is strings from user input



john bullwinkle
Greenhorn

Joined: Nov 04, 2010
Posts: 4
nvm. i forgot to put the /c flag in there.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
Please avoid abbreviations like "nvm".
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

And before we forget, When Runtime.exec() won't.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

You can use Execlibrary from the Apache Commons. Its pretty handy utility for running external programs from Java.


Mohamed Sanaulla | My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using windows command line to copy and paste. getting file not found error
 
Similar Threads
.sh file not found
Compile and create instance of a new java program from existing java program
Process
Reg:Executing Commands
Runtime getRuntime() exec(String command) - How does this work?