Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I run shell script within Java code?

 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I wonder is there is a 'correct' way of doing this.
Is it possible for me to run a shell script through a Java program?

For example;

class ScriptInJava {

String command = "ping www.google.com";

public static void main(String[] args) {

// Here I want to the run the string 'command' as I would run it on a terminal.

// Do more stuff here.

}
}

Is it possible to anything like this?

Thank you.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, check out the static exec() methods in the java.lang.Runtime class.
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks heaps Ernest.
I think exec() is definitely what I was looking for.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic