Solved

what is the command in java to clear console screen output?

Posted on 2009-04-01
13
4,867 Views
Last Modified: 2013-12-22
Hi

I have a console program written by java 1.5, it will display data when the input parameters change. However, I want to clear the screen and display new data.

What is the java code to clear the console screen?

0
Comment
Question by:techques
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
I think, the easy way is to print empty lines!
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
Nothing in the language (that I know about).
When I need it I use the OS level commands - check for OS and invoke the proper one.
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
>>the easy way is to print empty lines!
Won't this end up with you being at the bottom of the screen and your input starting from somewhere in mid-screen? Not that it is a bad idea per se - just wondering ;)
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
>>Won't this end up with you being at the bottom of the screen and your input starting from somewhere in mid-screen

true, it would!
0
 

Author Comment

by:techques
Comment Utility
check for OS and invoke the proper one

It runs on windows os (2000 and xp), how should it be?

0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
If it will run only on Windows

Runtime.getRuntime().exec("cls") or something like this should do the trick.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 20

Expert Comment

by:Venabili
Comment Utility
If you have issues with it on XP (seem to remember some but have no XP here now), you can also use "cmd /c cls" as a command.
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
>>check for OS and invoke the proper one

I don't think you can do that unless if there is a possibility with JNI/devicedriver but I doubt that but I would prefer you luanch your own console using Swing and then play there!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You can't control the console from pure Java. If you want more control, use JCurses
0
 
LVL 17

Expert Comment

by:Thomas4019
Comment Utility
I would print around 30 empty lines to clear the screen. Print your infornation, then print a few more empty lines to bring the information up on the window.

So basically. Print 30 empty lines, Print information, Print 10 empty lines (Depends on window size).

I am fairly sure that all of these "Runtime.getRuntime().exec(....." commands will not work because though they are proper windows commands or console functions, how would Windows know which command prompt to run them in. So Windows runs them in a new process, bascically clearing the screen of a new window.
0
 
LVL 6

Expert Comment

by:Tony O'Byrne
Comment Utility
Is it even wise to use OS level commands?  If you use the OS level commands, do you basically destroy any chances of the application being cross-platform?
0
 

Author Comment

by:techques
Comment Utility
Runtime.getRuntime().exec("cls") does not work
0
 
LVL 17

Accepted Solution

by:
Thomas4019 earned 50 total points
Comment Utility
Yes, I am fairly sure that wouldnt not work becuse that executes that command in a different process and in a hidden window.

Here's a link looking into ways to do what you want.

http://faq.javaranch.com/java/HowToClearTheConsole
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now