Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 11-22-2003, 01:42 AM   #1
LQ Newbie
Registered: Nov 2003
Location: mumbai-india
Posts: 6

Rep: Reputation: 0
Question how to invoke a linux command inside a java code ?

I want to execute my some linux commands like mkdir ,or any linux command, through my java code.
How do I do his ,can anybody tell me ?
if any one knows this ,can u plz send me an example of this.
Old 11-22-2003, 08:51 AM   #2
LQ Veteran
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 420Reputation: 420Reputation: 420Reputation: 420Reputation: 420
Um, why would you want to run an external command? That pretty much defeats the purpose of using Java in the first place. Besides, most commands, like mkdir, have Java equivalents. In fact, the Java equivalent of mkdir is, well, mkdir and it is in the class. Using the real Java methods to do those tasks means that your code is actually portable.
Old 11-23-2003, 02:19 PM   #3
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Debian etch, Gentoo
Posts: 312

Rep: Reputation: 30
Everything Hangdog said is true (except that mkDir() is a method of the class), this totally wrecks the platform neutrality of your application, but here is how to invoke native commands from within java:

public class Test {
  private static String[] command = new String[] {

  public static void main(String[] args) {
    try {
      Runtime rt = Runtime.getRuntime();
      Process p = rt.exec(command);
      // this is optional if you're holding for the native cmd to finish
      int n = p.waitFor(); // returns exit status of native code
    } catch(Exception e) {


Last edited by german; 11-23-2003 at 02:22 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
execute shell command inside of c code? khucinx Programming 6 10-26-2010 10:30 PM
Using [code] inside of [color] results in very small text scuzzman LQ Suggestions & Feedback 1 03-28-2005 03:14 AM
Java code to get inet address in linux qiye Programming 1 09-15-2004 02:23 PM
error when using a system command to invoke convert nodyl Programming 4 08-25-2004 11:00 PM
run shell command inside of c code? khucinx Programming 2 05-17-2004 11:04 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:21 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration