LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Execute a linux command using a java program (https://www.linuxquestions.org/questions/programming-9/execute-a-linux-command-using-a-java-program-370100/)

tvalenzuela 10-05-2005 03:52 PM

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

mrcheeks 10-05-2005 04:50 PM

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...)

tvalenzuela 10-07-2005 11:59 AM

Thanks I will go ahead and follow the instructions you provided, I think I am almost there


All times are GMT -5. The time now is 02:26 AM.