jonas garden Posted December 9, 2020 Posted December 9, 2020 I am creating console application of top command (Linux) through java, but the results are only given for just the instant you run the program, I don't know how to delay or refesh the results with time to be like in Linux for the top command (the result appears continuously and dynamic like in linux )
Ghideon Posted December 9, 2020 Posted December 9, 2020 33 minutes ago, jonas garden said: I am creating console application of top command (Linux) through java, but the results are only given for just the instant you run the program, I don't know how to delay or refesh the results with time to be like in Linux for the top command (the result appears continuously and dynamic like in linux ) It's hard to pinpoint your problem with the facts given but as a general approach you may have a look exiting code. The github project jvmtop seems to have a goal similar to yours; it may not be very active but could provide some hints about how to proceed. The method run(ConsoleView view) in class JvmTop.java and the output implementation in VMDetailView.java may be reasonable points to start. Note & Disclaimer: I am not a participant in the project and I have not used the code or done anything further check the quality of the code in JvmTop
Sensei Posted December 9, 2020 Posted December 9, 2020 (edited) There are special ANSI codes to control cursor position and clearing terminal, storing and restoring cursor position etc etc. https://en.m.wikipedia.org/wiki/ANSI_escape_code Write example code which is using them to check whether it is what you want. It is independent from used computer language. Edited December 9, 2020 by Sensei
Sensei Posted December 9, 2020 Posted December 9, 2020 3 hours ago, jonas garden said: you run the program, I don't know how to delay To delay refresh by 1 second try this: https://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html or this: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html
PoetheProgrammer Posted December 10, 2020 Posted December 10, 2020 Iirc (this may not be true but it is for most dynamic console commands) top uses ncurses which is a TUI library. It’s fairly easy to do this ncurses though my only experience with it is in C not Java, you may want to see if there are binding for that. The old school way is to simply redraw the entire console with only the characters you want changed changed. Normally you’d check how tall the current console is and ‘\n’ your way to a clear screen before redrawing, but you can use the clear command if you want (most languages have a system library that lets you call commands programmatically.)
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now