Solved

Run shell commands in java

Posted on 2001-06-10
12
1,165 Views
Last Modified: 2010-04-16
Is there a way to run shell commands in java?
0
Comment
Question by:rzvika
  • 5
  • 5
  • 2
12 Comments
 
LVL 15

Accepted Solution

by:
ozymandias earned 150 total points
ID: 6173010
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
 

Author Comment

by:rzvika
ID: 6173025
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
 
LVL 2

Expert Comment

by:Andrey_Kulik
ID: 6173065
you shuold write

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

Andrey
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6173227
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
 

Author Comment

by:rzvika
ID: 6186233
Is there a way to fun it minimized?
0
 
LVL 2

Expert Comment

by:Andrey_Kulik
ID: 6186616
This is one more question :)

It cost some points

Andrey
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:ozymandias
ID: 6186744
What do you mean "minimized" ? It's a command-line tool, what do you want to minimize ?
0
 

Author Comment

by:rzvika
ID: 6186749
I would like to know if there is a way to run it without the seeing the console window at all.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6186764
You canm try running it using javaw.exe instead of java.exe
0
 

Author Comment

by:rzvika
ID: 6186775
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
 

Author Comment

by:rzvika
ID: 6186819
I want this code to run also as an applet, so it won't help...
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6187148
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
json example 39 134
eclipse shortcuts 9 45
ForLoop Example 3 40
Why my table column Id is not passed to java object? 4 26
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

896 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now