This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I run shell script within Java code?

 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • 0
  • 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: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • 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
  • 0
  • 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