# Linux through Java code

**URL:** <https://community.unix.com/t/linux-through-java-code/315046>\
**Category:** Linux\
**Created:** [August 12, 2012, 8:21am UTC](https://community.unix.com/t/linux-through-java-code/315046 "2012-08-12T08:21:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dorothy](https://community.unix.com/letter_avatar/dorothy/32/6_449c533f586939b6e5f246632a46421f.png) [@Dorothy](https://community.unix.com/u/Dorothy)\
**Post date:** [August 12, 2012, 8:21am UTC](https://community.unix.com/t/linux-through-java-code/315046/1 "2012-08-12T08:21:54Z")

</div>

Hi

How can I write a Java program to execute Linux commands? What is the best approach:

- Invoking the Linux shell within Java and executing commands
- or, using the Java APIs to do the stuff. Since it is platform independent, it'll know itself what to do. We need not check which OS the Java code is executing on. Simply execute the command

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/6_449c533f586939b6e5f246632a46421f.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [August 19, 2012, 1:18pm UTC](https://community.unix.com/t/linux-through-java-code/315046/2 "2012-08-19T13:18:55Z")

</div>

It seems like the Runtime Class allows you to fork and exec off a child. [Runtime (Java Platform SE 6)](http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String)) A shell is needed to do complicated command lines and to push things to the background, but not for single command runs.
