Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-05-2005, 04:52 PM   #1
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

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

Rep: Reputation: 51
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 05:38 AM.
Old 10-07-2005, 12:59 PM   #3
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


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

All times are GMT -5. The time now is 01:15 PM.

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