Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

uptime command and windows

Status
Not open for further replies.

skarosi

Programmer
Jan 25, 2004
140
GR
Dear all,
i need to get some basic information for the computer that i run my programm.
i know that there is a command like for unix called "uptime" and it gets u all i need.
is there anything for windows? i found and downloaded an uptime command for windows, but it is not from MS, but that would b ok.
anyway, my java question now. i need to access this command "uptime" from inside the programm and to return a string. how can i do that?
i guess this is the same for all msdos command lines for java.
will it be the same if i run it in unix as well?
i must b a cross platform programm.
thanks, Ilias
 
I doubt you can give a cross-platform solution if you use native command lines. Well, maybe an if could do the trick.

Anyway, you should take a look at System.exec() command. Search for that in this forum, there are plenty of examples.

Cheers,

Dian
 
first of all thanks for your help,
now, i searched the forums for the System.exec(), but i didnt get anything usufull. there are only 3 posts for java, and they are not actually what i am looking for.

i also checked on the Sun API, but it doesnt even exist there.
could u just tell me how i can use this?
i tried
System.exec(uptime)
and
System.exec("uptime")

but they are both return an error
uptime is the command line in dos by the way.
thanks
 
Thanks both, but as i said in my first post, i want to read the result of the command, so Bong, the post Dian said is much better.
i have a problem with the InputStream() method, but i will find a way.
thanks again guys
 
OK, i love programming, and i think that i am quite good at it, when i know what i am doing, and i like java, but some times is sooo hard to realise what on earth is wrong!
like now, why do it get an error on this line:
BufferedReader br = new BufferedReader(new InputStreamReader(p.InputStream()));
on the InputStream()
i import java.io.*; where Input stream is.
what more does it need?
 
That's what APIs are for. I guess p is an instance of Process class, so the method you're looking for is not InputStream but getInputStream.

Cheers,

Dian
 
i am looking at the API, but cnat find anything! i was looking at the inputStream, and now on the getInputStream, they both uses java.io, so i import this, and also i try with java.net and java.lang, but nothing!
ilias
 
Incidentally, you would read the process output like so :

Code:
Process p= Runtime.getRuntime().exec("C:/full/path/to/uptime");
BufferedReader br = new BufferedReader(new InputStreamReader(p.InputStream()));
String line = "";
while ((line = br.readLine()) != null) {
  System.out.println(line)
}

--------------------------------------------------
Free Database Connection Pooling Software
 
ok, it worked out. it was a matter of restarting the dosprompt.
thanks for your help everybody
Ilias
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top