0

I wanted to write an application for windows/mac/linux and I chose java because a java developer friend told me Java is platform independent.

I came to a problem where I wanted to clear the console (to make the reading easier on the user).

I've tried System.out.flush(); and Runtime.getRuntime().exec("cls");.

It seems the only real solution is to use a hack (and smelly code is bad) - Java: Clear the console (check "Dyndrilliac" answer).

The reason using this hack is bad, as other users have commented on "Dyndrilliac" answer, what happens when Microsoft or Apple changes the command name from "cls" or "clear" to something different? It pretty much breaks your application and you get an error (such as "command not recognized") from the OS Company.

So my question is: did Java decide not to implement a library that handles clearing console outputs due to keeping it up to date whenever Windows/Macs/Linux decides to change it's command names? Or did Java implement a library for handling this, and I just can't find it?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83
  • 3
    Java is platform-independent *for the things it supports*. Console capabilities vary wildly, and Java tends to supporting lowest common denominator in the core API. – chrylis -cautiouslyoptimistic- Jun 04 '17 at 18:34
  • You could write e.g. a Swing program with a text pane or editor pane if you want absolute control over the console. – Radiodef Jun 04 '17 at 18:37
  • @chrylis so are you saying clearing the console provided by a platform OS is not considered to be a lowest common denominator in the core API? – Fiddle Freak Jun 04 '17 at 18:38
  • @Radiodef Yup, I agree. I am going to. I just wanted to create this application to have several interface types to practice separating my application and business rules from the interface (delivery mechanism). – Fiddle Freak Jun 04 '17 at 18:40
  • Even just Unix has wildly differing console capabilities. Look into `terminfo` for a peek into a historical labyrinth--and remember that Mac OS Classic had no "terminal" to speak of when Java was introduced. – chrylis -cautiouslyoptimistic- Jun 04 '17 at 19:42

2 Answers2

1

Java is independent in the way that it's not run directly on your OS. java run on jvm (java virtual machine) and because of that it should work the same in all OS. But the console that you print to it's the specific OS console and therefore, every OS will have it's own clear console command (because it's not the same console). You can build program with UI (using swing or other library) And control your output, or use some technics like in the post you linked to, it should work fine.

itay
  • 357
  • 4
  • 16
  • hmmm, so the best solution seems to having the os console first bring up the jvm console (using swing or whatever) and from there I can manage that console behavior using JVM's API methods. – Fiddle Freak Jun 04 '17 at 18:46
  • Seems somebody came by downvoting questions question and all answers; without giving any reasoning. I already upvoted the question; but lets restore things here, too. – GhostCat Jun 05 '17 at 17:13
0

A) because there are many many more platforms java is running on that might have extremely different views on what a console is.

B) in order to give decent tooling for console based applications, you need much more than simple "clear" commands (think nurses here!)

And finally: when Java came into existence, anything was about applets and ui. Probably nobody wanted to invest much energy in supporting "legacy style" command line programming. (that last point is probably closer to an opinion than a hard fact).

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Interesting, and slightly disappointing (yet understandable). – Fiddle Freak Jun 04 '17 at 18:41
  • Also I didn't realize there were different views on what a commandLine console is. I thought it was always understood as a delivery mechanism showing the I/O of text based commands. – Fiddle Freak Jun 04 '17 at 18:43
  • Sure, on a higher level that is true. But there is always a specific implementation. Or multiple ones. – GhostCat Jun 04 '17 at 19:02
  • Yup, right now I'm trying to decide if I want to create an abstract class that will detect the running OS, and have it's own clear methods (which is basically the hacking solution I talked about), or if I should just create a new console object entirely and go back and changing all the `System.out.print()` to something like `Console.print()` in the code for my current class. – Fiddle Freak Jun 04 '17 at 19:31