aspose file tools*
The moose likes Beginning Java and the fly likes Can I run shell script within Java code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can I run shell script within Java code?" Watch "Can I run shell script within Java code?" New topic
Author

Can I run shell script within Java code?

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
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

Joined: Jul 08, 2003
Posts: 24168
    
  30

Yes, check out the static exec() methods in the java.lang.Runtime class.


[Jess in Action][AskingGoodQuestions]
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thanks heaps Ernest.
I think exec() is definitely what I was looking for.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I run shell script within Java code?
 
Similar Threads
Running external application with stdin as argument
runtime exec
Running a shell script on diff m/c through java. Bartenders plzreply
problem with execution of script
Problem with Installation of Luntbuild