LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-22-2003, 01:42 AM   #1
kusum
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.
pleazzzzzzzzzzzzzzzz..........
thanx.
 
Old 11-22-2003, 08:51 AM   #2
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 416Reputation: 416Reputation: 416Reputation: 416Reputation: 416
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 java.io 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
german
Member
 
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 java.io.File class), this totally wrecks the platform neutrality of your application, but here is how to invoke native commands from within java:

Code:
public class Test {
  private static String[] command = new String[] {
    "mkdir",
    "bunk"
  };

  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
      System.exit(n);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}
HTH

B.

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


Reply


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:31 AM.

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