?
Solved

How to run system commands using java

Posted on 2009-12-26
15
Medium Priority
?
698 Views
Last Modified: 2013-12-14
For instance i need to run following command in MSDOS mode using java

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

0
Comment
Question by:Maranellas
  • 6
  • 3
  • 3
  • +2
15 Comments
 
LVL 6

Expert Comment

by:arevuri
ID: 26125043
Use System.exec to run os commands
0
 
LVL 5

Assisted Solution

by:afibarra
afibarra earned 800 total points
ID: 26125044
You can use for example:

Process p = Runtime.getRuntime().exec("C:");
0
 
LVL 6

Expert Comment

by:arevuri
ID: 26125050
sorry afibarra is correct
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Maranellas
ID: 26125051
u mean like this String cmd = System.getProperty("System.exec"); ?
0
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 26125065
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
 
LVL 6

Expert Comment

by:arevuri
ID: 26125067
no, copy your complete command  and pass as a String param to Runtime.getRuntime.exec method.
0
 
LVL 5

Expert Comment

by:afibarra
ID: 26125082
Now, arevuri is right... =)
0
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 26125117
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
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 26125130
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
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 26125137
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
 

Author Comment

by:Maranellas
ID: 26125187
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
 
LVL 92

Expert Comment

by:objects
ID: 26125622
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
 

Author Comment

by:Maranellas
ID: 26126195
doesn't make sense to me.Can you give an example with a code provided above?
0
 
LVL 12

Accepted Solution

by:
jazzIIIlove earned 1200 total points
ID: 26132382
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
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 26183686
So, what is the progress?:)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question