LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-05-2005, 03:52 PM   #1
tvalenzuela
LQ Newbie
 
Registered: Oct 2005
Posts: 2

Rep: Reputation: 0
Execute a linux command using a java program


I have an assignment where I need to write a program in java that will execute the following linux commands:


(1) read file “/proc/uptime” to obtain Totaltime and Idletime

(2) read file “/proc/sys/kernel/hostname” to obtain the hostname

(3) read file “/proc/version” to obtain the kernel version

(4) read file “/proc/meminfo” to obtain the amount of used memory and free memory

(5) read file “/proc/driver/rtc” to obtain time and date

I tried it using the Runtime.getRuntime(); but it doesn't give the info needed. It compiles without errors but that is about it.

A classmate said he used the BufferedRead class to access the different files and broke them down by lines. Then he used the StringTokenizer class to split the lines into word groups and was able to select the data that he wanted.
Does anyone have a sample program that will execute these commands

Thanks
 
Old 10-05-2005, 04:50 PM   #2
mrcheeks
Senior Member
 
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 50
I cannot do your homework for you see LQ rules but here are the steps :
- use runtime.exec after you get runtime.getRuntime() to execute the process(look at the javadoc of runtime class)
- read the process result from an inputstream(see the javadoc or look on the web how to use inputstream)
- use an inputstreamreader from the inputstream to prepare the next step(see the javadoc )
- use a bufferedreader created from the the inputstreamreader to read each lines(see the java tutorial or google)
- use a tokenizer while reading each lines to split the results(ex: result mem|100m|xxx...) see the javadoc
- do what you have to do with the results(print, write, etc...)

Last edited by mrcheeks; 10-06-2005 at 04:38 AM.
 
Old 10-07-2005, 11:59 AM   #3
tvalenzuela
LQ Newbie
 
Registered: Oct 2005
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks I will go ahead and follow the instructions you provided, I think I am almost there
 
  


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
C program code to execute linux-command supermichou_fr Linux - Software 4 08-01-2012 08:25 AM
How to run a command through a Java program kernelvn Programming 3 12-02-2004 09:25 PM
how to run linux command from java program? ranger225 Linux - Newbie 1 11-10-2004 06:55 AM
execute command when program closes frieza Linux - Software 3 11-17-2003 05:51 PM
Command line arguments to java program Majjj Programming 3 10-23-2003 05:48 AM


All times are GMT -5. The time now is 07:34 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration