Could anyone tell me if it's possible to clear console in intellij-idea and how?
So far I have tried:
System.out.print("\033[H\033[2J");
System.out.flush();
and:
Runtime.getRuntime().exec("clear");
but any of those did work.
I'm using Intellij-idea ultimate 2017 2.4 and Linux Mint.
Use Grep Console plugin for clearing it when it matches some output.
"Right" click over the console and select "clear all"
I don't believe you can, think of that window as a log file.
BTW, why would you want to clear that?
IntelliJ IDEA console is not a real terminal, so there is no command to clear it from your Java code.
See the issue here.
Try run your application in other terminal, then you can make a function like this:
public static void ClearConsole(){
try{
String operatingSystem = System.getProperty("os.name") //Check the current operating system
if(operatingSystem.contains("Windows")){
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "cls");
Process startProcess = pb.inheritIO.start();
startProcess.waitFor();
} else {
ProcessBuilder pb = new ProcessBuilder("clear");
Process startProcess = pb.inheritIO.start();
startProcess.waitFor();
}
}catch(Exception e){
System.out.println(e);
}
}
Related
I'm experiencing a weird behaviour of java's ProcessBuilder.
What I try is to stop a running screen using a shell, delete a few folders and after that restart the screen using another shell script.
The first step, killing the running screen, runs perfectly using:
ProcessBuilder pb0 = new ProcessBuilder(System.getProperty("user.dir") + "/generator/stop.sh");
In this stop.sh shell I simply run
screen -X -S generator kill
which works as it should.
After that I delete my directorys using org.apache.commons.io.FileUtils and then I want to start the screen again. Currently I'm doing it like that:
System.out.println("Restarting the generator");
ProcessBuilder pb1 = new ProcessBuilder();
pb1.directory(new File(System.getProperty("user.dir") + "/generator"));
pb1.command("./start.sh");
try {
Process process = pb1.start();
System.out.printf("Started the generator with %d", process.waitFor());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
This gives out "Started the generator with 1" which indicates to me that the screen runs which is simply not the case when checking with screen -ls.
No errors, no clue on how to move forward from here
Inside start.sh:
screen -S generator java -Xms2G -Xmx2G -jar generator.jar
PS: I'm using Debian 10.
Maybe anyone can help me out here?
Greets!
You should never ignore process output, because it's buffer has limited length, and if you don't consume it, it will hang. Im not sure if this is causing your issue, but this is definitely something you should do.
It is also possible that process throws some error that you can't see because you are ignoring it's output (in which case, this will help you to investigate the issue).
Try this:
new ProcessBuilder()
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT)
...
This will redirect process output stream and input stream to it's parent (which is your application).
So I have been trying to find a solution to this problem for quite a while now but haven't been able to even get an error message. I am trying to toggle the dark mode on MacOS with this command: /usr/local/bin/dark-mode on
It works if I execute it with the standard terminal, but executing the code below doesn't do anything. The log file is empty. To make sure my code is correct I used a different command (commented out) and this one gave me the correct output to whoami and returned my current user.
I thought this may have to do with application security on MacOS? I have no idea where to start though.
private void switchDark() {
try {
//Activate the dark mode on MacOS
String command = new String[] { "/usr/local/bin/dark-mode", "on" };
//String command = new String[] { "whoami" }; //This works and gives me the current user
ProcessBuilder pb =
new ProcessBuilder(command[0], command[1]);
File log = new File("LOG.txt");
pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process proc = pb.start();
proc.waitFor();
if(proc.exitValue() != 0) {
throw new IllegalThreadStateException();
}
} catch (IOException | InterruptedException | IllegalThreadStateException ex) {
System.out.println("Setting dark mode failed!");
if(debug)System.out.println(ex.getMessage());
}
}
I looked at the code to see what that "dark-mode" command does. (https://github.com/sindresorhus/dark-mode - thanks #MadProgrammer)
I'm no Swift or MacOS expert, but that command's implementation doesn't look like it tests if the AppleScript command succeeded. Furthermore, there doesn't seem to be anything to set non-zero exit status in the event of a failure.
So my conclusion is that the underlying AppleScript command is not working ... for reasons that are not reported ... and that information is not passed back to Java via the exit status. There is nothing you can do about it at the Java level.
My guess is that the "dark mode" request is actually failing for permissions-related reasons.
I just started coding in Java and i'm trying to make a program to run chkdsk when i click on a JButton. I will put a bit of the code here so you guys can help me:
String disk = JOptionPane.showInputDialog(Janela, "Especifique a letra do disco (Exemplo: C:)", "CHKDSK /F", JOptionPane.QUESTION_MESSAGE);
if (disk.length() == 2 && disk.endsWith(":")) {
try {
String disk2 = ("fsutil dirty set " + disk)
ProcessBuilder chkdskf = new ProcessBuilder("cmd.exe", "/C", "start", disk2);
Process chkdskff = chkdskf.start();
}
catch (IOException fnfex2) {
System.out.println ("Erro no CHKDSK /F");
}
}
else {
JOptionPane.showMessageDialog(Janela, "Erro!", "Erro", JOptionPane.ERROR_MESSAGE);
}
So, it shows a InputDialog so you can put a drive letter (like C: or D:), and after that it checks if the string is the way i want. (Has two characters and ends with a ":"). Then, it starts a new cmd window with the command to make chkdsk run on the next reboot. However, it doesn't work. The CMD window that is opened when the code is executed has the title of "fsutil dirty set C:", but nothing happens, no command is executed. Any help is appreciated, and sorry for my poor english.
From the command line help for start
STATE ["title"] [/D path] ... [command/program] [parameters]
So, based on that, it would mean that start is taking fsutil dirty set C: as the title - the reason is because of the way the parameters work in ProcessBuilder.
Each element in the array is a separate argument been sent to the command, this is really helpful as it means you don't need to worry about quotes or other escaping requirements
So, based on all of this, you should construct your ProcessBuilder more like...
ProcessBuilder chkdskf = new ProcessBuilder("cmd.exe", "/C", "start", "Make it so", "fsutil", "dirty", "set", disk);
I don't know the way you are implemented. But I have an idea for your refer. Let's create a .bat file, write the execute statement to this file, then call to run this .bat file like this:
Runtime.getRuntime().exec("cmd /c start " + "command.bat");
I am working on a game that involves clearing the screen after each turn for readability. The only problem is I cannot use the Windows command prompt-based "cls" command and it does not support ANSI escape characters. I used Dyndrilliac's solution on the following page but it resulted in an IOException:
Java: Clear the console
Replacing "cls" with "cmd \C cls" only opened a new command prompt, cleared it, and closed it without accessing the current console. How do I make a Java program running through Windows Command Prompt access the command prompt's arguments and use them to clear its output?
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
Solved here: Java: Clear the console
I realize this is an old post, but I hate when I find questions with responses of never mind i got it, or it just dies off. Hopefully it helps someone as it did for me.
Keep in mind it won't work in eclipse, but will in the regular console. take it a step further with if you're worried about cross OS:
final String os = System.getProperty("os.name");
if (os.contains("Windows"))
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
else
Runtime.getRuntime().exec("clear");
public static void clrscr(){
//Clears Screen in java
try {
if (System.getProperty("os.name").contains("Windows"))
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
else
Runtime.getRuntime().exec("clear");
} catch (IOException | InterruptedException ex) {}
}
There's pretty much nothing in the console related API to do a clear screen. But, you can achieve the same effect through println()s. A lot of putty clients clear the page like that and then scroll up.
private static final int PAGE_SIZE = 25;
public static void main(String[] args) {
// ...
clearScreen();
}
private static void clearScreen() {
for (int i = 0; i < PAGE_SIZE; i++) {
System.out.println();
}
}
Create a batch file to clear the cmd screen and run your java program
Step 1. Create a file with extension .bat
Step 2. So your code in batch file will be
Cls
Cd desktop // path
Javac filename.java // compiling
Java desk // running
By doing this....you can clear the screen during run time
I am facing a weird issue with executing a system command from JAVA code.
Actually i want to get the Mac OSX system information from my JAVA App.
For that im using
Runtime.getRuntime().exec("system_profiler -detailLevel full");
This is working fine.If i print the output,it is cool.
But i want to write this information to a plist file for future use.For that im using the -xml argument of system_profiler.like,
String cmd = "system_profiler -detailLevel full -xml > "+System.getProperty( "user.home" )+"/sysinfo.plist";
Process p = Runtime.getRuntime().exec(cmd);
Basically this should create a plist file in the current users home directory.
But this seems to be not writing anything to file.
Am i missing something here ?
My Java is more than rusty, so please be gentle. ;-)
Runtime.exec() does not automatically use the shell to execute the command you passed, so the IO redirection is not doing anything.
If you just use:
"/bin/sh -c system_profiler -detailLevel full > path/file.plist"
Then the string will be tokenized into:
{ "/bin/sh", "-c", "system_profiler", "-detailLevel", "full", ">", "path/file.plist" }
Which also wouldn't work, because -c only expects a single argument.
Try this instead:
String[] cmd = { "/bin/sh", "-c", "system_profiler -detailLevel full > path/file.plist" };
Process p = Runtime.getRuntime.exec(cmd);
Of course, you could also just read the output of your Process instance using Process.getInputStream() and write that into the file you want; thus skip the shell, IO redirection, etc. altogether.
Christian.K is absolutely correct. Here is a complete example:
public class Hello {
static public void main (String[] args) {
try {
String[] cmds = {
"/bin/sh", "-c", "ls -l *.java | tee tmp.out"};
Process p = Runtime.getRuntime().exec (cmds);
p.waitFor ();
System.out.println ("Done.");
}
catch (Exception e) {
System.out.println ("Err: " + e.getMessage());
}
}
}
If you weren't using a pipe (|) or redirect (>), then you'd be OK with String cmd = "ls -l *.java", as in your original command.
If you actually wanted to see any of the output in your Java console window, then you'd ALSO need to call Process.getInputStream().
Here's a good link:
Running system commands in Java applications