10 Replies - 26193 Views - Last Post: 16 September 2010 - 01:42 AM Rate Topic: -----

#1 kevin_06s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-January 10

Java Clearing Console Screen

Posted 14 September 2010 - 06:48 PM

I Googled for a way to clear the console screen. The general answer was that it was not possible to do so platform independently, but from what I see it is possible to make it platform independent.

I think one could first detect the OS the the user is running, then call the native console clearing function for each OS. I have the OS detection, but I don't know how to call the native console clearing functions, please help.
public class Clear{
	public static void main(String[] args){
		if(isWindows()){
			//Windows
		}else if(isMac()){
			//Mac
		}else if(isLinux()){
			//Linux
		}else{
			System.out.println("Your OS is not support!!");
		}
	}
 
	public static boolean isWindows(){
 
		String os = System.getProperty("os.name").toLowerCase();
	    return (os.indexOf("win") >= 0); 
	}
 
	public static boolean isMac(){
 
		String os = System.getProperty("os.name").toLowerCase();
	    return (os.indexOf("mac") >= 0); 
	}
 
	public static boolean isUnix(){
 
		String os = System.getProperty("os.name").toLowerCase();
	    return (os.indexOf("linux") >=0);
	}
}


This post has been edited by macosxnerd101: 14 September 2010 - 06:51 PM
Reason for edit:: Added code tags. Please use them in the future.


Is This A Good Question/Topic? 0
  • +

Replies To: Java Clearing Console Screen

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Java Clearing Console Screen

Posted 14 September 2010 - 06:53 PM

For win the clear command is cls
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10667
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: Java Clearing Console Screen

Posted 14 September 2010 - 06:56 PM

You can't clear the console screen guaranteed on all major platforms. I remember pbl, our resident Java authority, tried to write a wrapper API for the console to do just this, and didn't finish it, as best I recall b/c it was so difficult. Honestly, if you are still concerned with console applications, which aren't used in the real world, then this isn't a project you want to tackle. And if you are beyond console applications, then no need to really worry about them.

Perhaps, as H3R3T1C said, you can use some command line commands for certain OS's. But this is no guarantee you will be able to accomplish this task and keep your code platform independent.
Was This Post Helpful? 0
  • +
  • -

#4 kevin_06s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-January 10

Re: Java Clearing Console Screen

Posted 14 September 2010 - 07:09 PM

View Postmacosxnerd101, on 14 September 2010 - 05:56 PM, said:

You can't clear the console screen guaranteed on all major platforms. I remember pbl, our resident Java authority, tried to write a wrapper API for the console to do just this, and didn't finish it, as best I recall b/c it was so difficult. Honestly, if you are still concerned with console applications, which aren't used in the real world, then this isn't a project you want to tackle. And if you are beyond console applications, then no need to really worry about them.

Perhaps, as H3R3T1C said, you can use some command line commands for certain OS's. But this is no guarantee you will be able to accomplish this task and keep your code platform independent.


So I suppose I should just give up and stick with the newline hack?
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Java Clearing Console Screen

Posted 14 September 2010 - 07:09 PM

Read me.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10667
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: Java Clearing Console Screen

Posted 14 September 2010 - 07:10 PM

Yes. If you are still on console apps, then you will want to stay clear of native code. The JNI isn't the easiest to work with.
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Java Clearing Console Screen

Posted 14 September 2010 - 07:11 PM

Plus, even for regular apps, going down to the native level defeats the purpose of one of Java's strengths in the first place.
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Java Clearing Console Screen

Posted 14 September 2010 - 07:30 PM

What you have typically is the hack that I use for things like clearing the screen, shutting down, and OS dependent things like that. The one thing you have to know is for each of the three big OS's what the command line options are, and then have a backup plan if none work (like if it's some random OS). Here's how I do it.

 
if (windows) 
    Runtime.getRuntime().exec("cls");
else if (linux)
    Runtime.getRuntime().exec("clear");
else if (mac)
    Runtime.getRuntime().exec("clear");

// If all else fails, print 100 blank lines
else {
    for (int i = 0; i < 100; i++) {
        System.out.println();
    }
}


Was This Post Helpful? 3
  • +
  • -

#9 kevin_06s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-January 10

Re: Java Clearing Console Screen

Posted 14 September 2010 - 07:50 PM

View PostDogstopper, on 14 September 2010 - 06:30 PM, said:

What you have typically is the hack that I use for things like clearing the screen, shutting down, and OS dependent things like that. The one thing you have to know is for each of the three big OS's what the command line options are, and then have a backup plan if none work (like if it's some random OS). Here's how I do it.

 
if (windows) 
    Runtime.getRuntime().exec("cls");
else if (linux)
    Runtime.getRuntime().exec("clear");
else if (mac)
    Runtime.getRuntime().exec("clear");

// If all else fails, print 100 blank lines
else {
    for (int i = 0; i < 100; i++) {
        System.out.println();
    }
}



Dogstopper++

This is exactly what I was looking for, thanks so much.
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Java Clearing Console Screen

Posted 14 September 2010 - 07:52 PM

Glad I could help. good luck.
Was This Post Helpful? 0
  • +
  • -

#11 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2045
  • View blog
  • Posts: 4,233
  • Joined: 11-December 07

Re: Java Clearing Console Screen

Posted 16 September 2010 - 01:42 AM

Unfortunately they broke that method in a recent java release. When you call exec (at least on windows) you get a new process which does not share the same console. I think cls is also a console command and not an application so you also can't launch cls from exec either.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1