Run shell commands in java

Is there a way to run shell commands in java?
rzvikaAsked:
Who is Participating?
 
ozymandiasConnect With a Mentor Commented:
Here is an example :

ShellExec.java
=========================================================

import java.io.*;

public class ShellExec{

     public static void main(String args[]){

          if (args.length < 1){
               System.out.println("Usage : ShellExec command");
               return;
          }

          String s = File.separator;
          String responses = new String("");
          System.out.println("Running : " + args[0]);
          Runtime rt = Runtime.getRuntime();
          try{
               Process p = rt.exec(args[0]);
               BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
               byte[] buffer = new byte[1];
               int eof = bis.read(buffer);
               while(-1 != eof){
                    responses = responses.concat(new String(buffer));
                    buffer = new byte[1];
                    eof = bis.read(buffer);
               }
               System.out.println(responses.trim());
               bis.close();
               buffer = null;
          }catch(Exception e){
               System.err.println(e.getMessage());
               e.printStackTrace(System.err);
          }
    }
}
0
 
rzvikaAuthor Commented:
I've tried to invoke the main method with args as:
new String[]{"dir"}

but there was an exception:
reateProcess: dir  error=2
java.io.IOException: CreateProcess: dir  error=2
     at java.lang.Win32Process.<init>(Win32Process.java:64)
     at java.lang.Runtime.execInternal(Native Method)
     at java.lang.Runtime.exec(Runtime.java:272)
     at java.lang.Runtime.exec(Runtime.java:195)
     at java.lang.Runtime.exec(Runtime.java:152)

0
 
Andrey_KulikCommented:
you shuold write

new String[]{"cmd /c dir"}

Andrey
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
ozymandiasCommented:
Andrey_Kulik is right. Dir is not a program (i.e. there is no dir.exe) it is a function/command of the command shell (cmd.exe).

You can run it like this :

java ShellExec "cmd.exe /c dir"

Cheers.
0
 
rzvikaAuthor Commented:
Is there a way to fun it minimized?
0
 
Andrey_KulikCommented:
This is one more question :)

It cost some points

Andrey
0
 
ozymandiasCommented:
What do you mean "minimized" ? It's a command-line tool, what do you want to minimize ?
0
 
rzvikaAuthor Commented:
I would like to know if there is a way to run it without the seeing the console window at all.
0
 
ozymandiasCommented:
You canm try running it using javaw.exe instead of java.exe
0
 
rzvikaAuthor Commented:
when I run the command "cmd /C set" a console window is opened.
I don't want it to happen.
I would like it not to be shown or if it not possible, to be minimized (the console window)
0
 
rzvikaAuthor Commented:
I want this code to run also as an applet, so it won't help...
0
 
ozymandiasCommented:
You will not be able to run commands from within an applet unless it is signed and trusted.

Can you explain a bit more about what you are trying to do ?
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.