Clearing The Screen?

Can this be done?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 62660 Views - Last Post: 18 April 2010 - 11:18 PM Rate Topic: -----

#1 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Clearing The Screen?

Post icon  Posted 21 December 2004 - 10:41 AM

Hey people,
I'm just talking about the Java Language being used for console output. Can we clear the screen like how we can in C++ by just the clrscr(); call??

If not is there a gotoxy() function in Java? Because if there is one, the following can be done:
1) Output a Blank on 80 Lines
2) gotoxy(0,0);


can Anybody help me?

Thanx,
Sanchit
Is This A Good Question/Topic? 0
  • +

Replies To: Clearing The Screen?

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Clearing The Screen?

Posted 21 December 2004 - 10:54 AM

Good question. There is no clear screen command for console apps in java, unless one has been developed recently (I'm doubtful). That's the price one has to pay for platform independance. As noted, you can simulate it by ourputting several endlines or spaces, but then your cursor is at the bottom of the screen. there is a java implementation of the ncurses library here which may be of some help, maybe not.
Was This Post Helpful? 0
  • +
  • -

#3 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4495
  • View blog
  • Posts: 28,404
  • Joined: 21-March 01

Re: Clearing The Screen?

Posted 21 December 2004 - 11:09 AM

When I used to program in Java I used a command to do it but I'm pretty sure it was library specific. When I get home tonight I'll check some of my leftover code and to verify for you though.
Was This Post Helpful? 0
  • +
  • -

#4 stdunbar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 17-December 04

Re: Clearing The Screen?

Posted 21 December 2004 - 01:55 PM

You're likely to be better off calling a O/S specific command. Something like:

String clearScreenCommand = null;
if( System.getProperty( "os.name" ).startsWith( "Window" ) )
    clearScreenCommand = "cls";
else
    clearScreenCommand = "clear";

Runtime.getRuntime().exec( clearScreenCommand );



This is not perfect but the task you want to do is somewhat O/S specific. However, the advantage is that you do not need to assume a particular screen size and configuration.
Was This Post Helpful? 1
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Clearing The Screen?

Posted 22 December 2004 - 06:25 AM

Yeah, that's always an option, just keep in mind you'll lose the independant nature of Java. Of course, you can try and take into consideration as many OS specific commands as you can. Point in fact, I'm sure most of your users will be runiing one of the major OS's to begin with, so you could probably just get away with the standards.
Was This Post Helpful? 0
  • +
  • -

#6 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Clearing The Screen?

Post icon  Posted 22 December 2004 - 09:20 AM

Well...i guess that code should work for Windows...thanks anyway
Was This Post Helpful? 0
  • +
  • -

#7 stdunbar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 17-December 04

Re: Clearing The Screen?

Posted 22 December 2004 - 02:42 PM

born2c0de, on Dec 22 2004, 09:20 AM, said:

Well...i guess that code should work for Windows...thanks anyway

The code should work for Windows and the vast majority of Unix variants (I'm sure it'll work on Linux, BSD, and Solaris). What it won't work on is OS/400, MVS, and other O/S's that would require a switch statement type of construct. Sorry, but my CICS is pretty rough and I'm not sure how to clear the screen on a 3270 terminal :blink:
Was This Post Helpful? 0
  • +
  • -

#8 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Clearing The Screen?

Posted 03 January 2005 - 03:19 AM

thanks...code works fine with Windows and Linux.
Was This Post Helpful? 0
  • +
  • -

#9 PsiloVybes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-March 05

Re: Clearing The Screen?

Posted 12 March 2005 - 05:41 PM

I was also wondering how to do this... but now it seems ive ran into a differnt problem

When i try the code posted by stdunbar the JVM throws an IOException. Does anyone know why this might be happening?
Was This Post Helpful? 0
  • +
  • -

#10 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Clearing The Screen?

Posted 12 March 2005 - 10:58 PM

Which OS are you using?
Was This Post Helpful? 0
  • +
  • -

#11 PsiloVybes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-March 05

Re: Clearing The Screen?

Posted 13 March 2005 - 04:03 PM

windows xp
Was This Post Helpful? 0
  • +
  • -

#12 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Clearing The Screen?

Posted 13 March 2005 - 09:51 PM

i guess just try this code out:
Runtime.getRuntime().exec("cls");


Was This Post Helpful? 1

#13 PsiloVybes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-March 05

Re: Clearing The Screen?

Posted 13 March 2005 - 10:30 PM

that is the line that is throwing the exception. thows the same IOException just by itself too.
Was This Post Helpful? 0
  • +
  • -

#14 kld2006  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-October 06

Re: Clearing The Screen?

Posted 06 October 2006 - 08:12 AM

View Postborn2c0de, on 21 Dec, 2004 - 10:41 AM, said:

Hey people,
I'm just talking about the Java Language being used for console output. Can we clear the screen like how we can in C++ by just the clrscr(); call??

If not is there a gotoxy() function in Java? Because if there is one, the following can be done:
1) Output a Blank on 80 Lines
2) gotoxy(0,0);


can Anybody help me?

Thanx,
Sanchit



View Postborn2c0de, on 21 Dec, 2004 - 10:41 AM, said:

Hey people,
I'm just talking about the Java Language being used for console output. Can we clear the screen like how we can in C++ by just the clrscr(); call??

If not is there a gotoxy() function in Java? Because if there is one, the following can be done:
1) Output a Blank on 80 Lines
2) gotoxy(0,0);


can Anybody help me?

Thanx,
Sanchit



for (int i =0;i<400;i++)
System.out.println("");
Was This Post Helpful? 0
  • +
  • -

#15 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Clearing The Screen?

Posted 29 March 2007 - 10:01 AM

I'm bumping this because I'm getting the same exception using Windows XP and I didn't see a resolution. In this situation, I don't care about platform independence. I'm just making a very quick and very dirty console app to test something.

This code
		try {
			Runtime.getRuntime().exec("cls");
		} catch (IOException e) {
			e.printStackTrace();
		}



Yields this exception

java.io.IOException: CreateProcess: cls error=2
		at java.lang.ProcessImpl.create(Native Method)
		at java.lang.ProcessImpl.<init>(Unknown Source)
		at java.lang.ProcessImpl.start(Unknown Source)
		at java.lang.ProcessBuilder.start(Unknown Source)
		at java.lang.Runtime.exec(Unknown Source)
		at java.lang.Runtime.exec(Unknown Source)
		at java.lang.Runtime.exec(Unknown Source) ... 


Anyone?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3