LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using Java runtime (https://www.linuxquestions.org/questions/programming-9/using-java-runtime-861903/)

kaylachris 02-10-2011 03:51 PM

Using Java runtime
 
I'm new to the linux world and its been quit sometime since i've done any programming. However, I'm writing a program which simply calculates the load average of a process. In doing this I need to use the uptime command for linux in a java program. I've done a little bit of searching on the net and it mentions this is possiable by using java runtime command. Unfortunatly though I have yet to find a working example of this. I've tried just simply reading the /proc/uptime file but I have no clue how to format the 2 numbers in seconds to make it the same as if you just typed in uptime in the linux command prompt.

I would love to post my code that i have so far but once agian i'm new to linux and haven't figured out how to download files from the server yet so if someone would help me out there it be greatly appreciated.

I'm lost... please help!

paulsm4 02-10-2011 04:02 PM

Easier still: just use Java I/O. Open and read from /proc/uptime exactly as you would any text file.

Simple as that :)

kaylachris 02-11-2011 01:42 AM

Quote:

Originally Posted by paulsm4 (Post 4254491)
Easier still: just use Java I/O. Open and read from /proc/uptime exactly as you would any text file.

Simple as that :)

I've tried that but not sure what the numbers mean nor how to format them correctly... for instance if I type in uptime in the linux command prompt it provides the time in this format
01:22:05 up 223 days, 4:57, 1 user, load average: 0.00, 0.00, 0.00

However if I use the following function to read/print the /proc/uptime file
Code:

public void printFile(String fileName){
  FileInputStream fstream = new FileInputStream(fileName);
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new bufferedReader(new InputStreamREader(in));
  String strLine;
 
  //Read file line by line
  while((strLine = br.readLine()) != null){
    System.out.println(strLine);
    }
  in.close();//closes input stream
  }catch (Exception e){
    System.err.println("Error: " + e.getMessage());
    }
}//end printFile

I get the just 2 numbers 19285670.75 2073995.21
I've figured out that the first number represents the uptime in seconds... not sure what the second one is though(i'm assuming its idletime?) nor how to format these so that they appear the same as using uptime directly from the shell... better yet I need to be able to use these times for calculation.
Here is the basic skeleton of the program of what I want it to do:
-read /proc/uptime; store as startTotaltime & startIdletime
-call a function that places a load on the system (really it will just be some random math calculation over and overagian)
-read /proc/uptime agian; store as endTotaltime & endIdletime
-Calculate the percentage of time that CPU was busy during the program


All times are GMT -5. The time now is 12:22 PM.