Visit Jeremy's Blog.
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 02-10-2011, 03:51 PM   #1
LQ Newbie
Registered: Feb 2011
Posts: 3

Rep: Reputation: 0
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!
Old 02-10-2011, 04:02 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

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

Simple as that
Old 02-11-2011, 01:42 AM   #3
LQ Newbie
Registered: Feb 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Originally Posted by paulsm4 View Post
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
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){
  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


uptime, [java]

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
[SOLVED] Java Woes: A Java Runtime Environment (JRE) or Java Development Kit (JDK) must be available ... chytraeus Slackware 10 11-27-2010 10:04 AM
Java runtime error. Exception in thread "main" java.lang.NoClassDefFoundError: / Priyadivya Red Hat 2 10-26-2010 05:01 AM
Java Programming: Java Runtime Environment not found when trying to compile murbz Linux - Software 2 03-26-2009 03:04 AM
Runtime.getRuntime().exec("cd") returns, error 2 in java tatarin Programming 4 03-28-2008 07:42 PM
How can I use Java runtime? annemarietjuhhh Linux - Newbie 8 02-28-2006 02:41 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:09 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