How to run system commands using java

For instance i need to run following command in MSDOS mode using java

"xcopy C:\inetpub\www\maintenance.log" C:\Logs\Maintenance\maintenance.log"

MaranellasAsked:
Who is Participating?
 
jazzIIIloveConnect With a Mentor Commented:
I have used copy as you see in the snippet and it's 100% working in Windows env.
Note that I haven't used xcopy since it's a little different as you examine the link:
http://www.computerhope.com/xcopyhlp.htm

You can adapt your xcopy command to copy multiple files or so to the following code
The default cmd location is C:\Windows\System32
If it's different in your OS, which I don't think so, I recommend editting the line cmd as

C:\Windows\System32\cmd or cmd (in my code snippet)
to:
your/path/to/cmd

Best regards.

Best regards.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cmd;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Baran
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            // TODO code application logic here
        Runtime systemShell = Runtime.getRuntime();
     //Process output = systemShell.exec("cmd.exe");
       Process output = systemShell.exec("cmd /c copy C:\\asdf.txt C:\\fdsa.txt");
     // open reader to get output from process

     BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream()));
     String line = null;
      System.out.println("Copy Process");
      while((line = br.readLine()) != null )
         { System.out.println(line);  }          // display process output

     int exitVal = output.waitFor();             // get process exit value
     System.out.println("Process Exit Value : "+ exitVal);

        }
        catch (Exception ex) {
            System.out.println("Better to learn try-catch stuff");
        }

    }

}

Open in new window

0
 
arevuriCommented:
Use System.exec to run os commands
0
 
afibarraConnect With a Mentor Commented:
You can use for example:

Process p = Runtime.getRuntime().exec("C:");
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
arevuriCommented:
sorry afibarra is correct
0
 
MaranellasAuthor Commented:
u mean like this String cmd = System.getProperty("System.exec"); ?
0
 
jazzIIIloveCommented:
Hi there;

String command = "cp C:\\inetpub\\www\\maintenance.log" C:\\Logs\\Maintenance\\maintenance.log"
Process p = Runtime.getRuntime().exec(command);

Be careful with \, it's the escape char. so, you need to use \\ or / in the string.

Best regards
0
 
arevuriCommented:
no, copy your complete command  and pass as a String param to Runtime.getRuntime.exec method.
0
 
afibarraCommented:
Now, arevuri is right... =)
0
 
jazzIIIloveCommented:
My mistake:
cp is in linux; we must use copy command.

String command = "copy C:\\inetpub\\www\\maintenance.log" C:\\Logs\\Maintenance\\maintenance.log";
Process p = Runtime.getRuntime().exec(command);
0
 
jazzIIIloveCommented:
or, let's do as follows:
String[] command = new String[2];
command[0] = "cmd";
command[1] = "copy C:\\inetpub\\www\\maintenance.log" C:\\Logs\\Maintenance\\maintenance.log";

Process p = Runtime.getRuntime().exec(command);

Open in new window

0
 
jazzIIIloveCommented:
I am a little distracted today:)
change the line from:
"copy C:\\inetpub\\www\\maintenance.log" C:\\Logs\\Maintenance\\maintenance.log";
to:
"copy C:\\inetpub\\www\\maintenance.log  C:\\Logs\\Maintenance\\maintenance.log";

String[] command = new String[2]; 
command[0] = "cmd"; 
command[1] = "copy C:\\inetpub\\www\\maintenance.log  C:\\Logs\\Maintenance\\maintenance.log"; 
 
Process p = Runtime.getRuntime().exec(command);

Open in new window

0
 
MaranellasAuthor Commented:
ERROR
unreported exception java.io.IOException; must be caught or declared to be thrown
Process p = Runtime.getRuntime().exec(command);
1 error
1 warning

Open in new window

0
 
objectsCommented:
When using Runtime.exec() you need to read any output from the command to avoid it locking up

http://helpdesk.objects.com.au/java/runtime-exec-locks-up
0
 
MaranellasAuthor Commented:
doesn't make sense to me.Can you give an example with a code provided above?
0
 
jazzIIIloveCommented:
So, what is the progress?:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.