Solved

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

Posted on 2009-04-01
13
5,210 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
ID: 24039518
I think, the easy way is to print empty lines!
0
 
LVL 20

Expert Comment

by:Venabili
ID: 24039543
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
ID: 24039554
>>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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 26

Expert Comment

by:ksivananth
ID: 24039723
>>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
ID: 24039782
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
ID: 24039883
If it will run only on Windows

Runtime.getRuntime().exec("cls") or something like this should do the trick.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 24039916
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
ID: 24040022
>>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
ID: 24040162
You can't control the console from pure Java. If you want more control, use JCurses
0
 
LVL 17

Expert Comment

by:Thomas4019
ID: 24044164
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
ID: 24045352
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
ID: 24069960
Runtime.getRuntime().exec("cls") does not work
0
 
LVL 17

Accepted Solution

by:
Thomas4019 earned 50 total points
ID: 24073568
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 53
difference between sorce folder and folder in eclipise 3 29
mysql jsp example issue 32 53
Need Help! Getting a syntax error and don't understand why 3 31
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

821 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