LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 12: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, 07:51 AM   #2
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
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, 01: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 01:22 PM.
 
  


Reply



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 09:30 PM
Using [code] inside of [color] results in very small text scuzzman LQ Suggestions & Feedback 1 03-28-2005 02:14 AM
Java code to get inet address in linux qiye Programming 1 09-15-2004 01:23 PM
error when using a system command to invoke convert nodyl Programming 4 08-25-2004 10:00 PM
run shell command inside of c code? khucinx Programming 2 05-17-2004 10:04 AM

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

All times are GMT -5. The time now is 10:47 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
Open Source Consulting | Domain Registration