#I need help making my console not close

1 messages · Page 1 of 1 (latest)

sonic storm
#

how do i make this console not close

public class Console {

}
class CommandLineExample
{
public static void main ( String [] arguments )
{
System.out.println("Elystro.js");
}
}

mortal bridgeBOT
#

Hey, @sonic storm!
Please remember to /close this post once your question has been answered!

sonic storm
#

no

#

how do i make

#

public class Console {

}
class CommandLineExample
{
public static void main ( String [] arguments )
{
System.out.println("Elystro.js");
}
}

#

liike not close when i open it

#

it stays pen forever until i press the close button

subtle finch
#

That's not under Java's control

#

Open the console first and run the program in then. Can be done with a Windows shortcut

sonic storm
#

dude

#

im making a console in java

#

thats all

#

on gui or application

#

just console by itself

compact spire
#

I don't know what IDE you're using, because most have the console integrated into the same window.
Try doing System.in.read() at the end

dusky sigil
#

The console is closing because the program has ended. If you want to keep the program running you have to do something to make it keep running such as in.read as suggested above, which indefinitely await input from stdin unless/until you give some input

compact spire
#

It's still weird that the console is closing

#

Every IDE I know of keeps the output

graceful aurora
graceful aurora
compact spire
#

Wdym?
System.out.println

#

Or do you want Eclipse's source code for the console widget?

graceful aurora
graceful aurora
subtle finch
# compact spire Every IDE I know of keeps the output

Well, IDEs are aware that they're being used by people currently trying to develop something and who need the potential feedback to stay.
Windows consoles, not so much, though I wouldn't deny if someone said that's another of Microsoft's stupid decisions

compact spire
#

I assumed this was being run from an IDE

#

Does it involve a batch script?

#

(to OP)

sonic storm
#

nah

#

i managed to fix it

#

just made a gui

#

then set visibility to false

sonic storm
#

thanks tho

atomic iris
#

might use less resources i think.

sonic storm
#

ok

atomic iris
#

ExecutorService console = Executors.newFixedThreadPool(1);

#
console.execute(() -> {
  try {
 while (true) {
 Thread.sleep(1);
}

}
catch (InterruptedException e) {
  e.prinStackTrace;
 }
});
compact spire
#

Why would you use a thread pool?

#

and you shouldn't use (Throwable::/e.)printStackTrace

compact spire
#

Well, I guess it's fine for small programs

#

but having a thread that always waits I don't think is considered good practice

atomic iris
#

because that's essentially what they're doing anyway when they create a gui

atomic iris
compact spire
#

If there's just one though

#
new Thread(() -> {
}).start();
atomic iris
#

while (!console.isTerminated()) {}

#

control flow in main with a threadpool

compact spire
#

Why would you do it in another thread though?

#

Why not the main thread?

atomic iris
#

how?

compact spire
#
BufferedReader reader = new BufferedReader(System.in);
for(;;) {
    String input = reader.readLine();
    if(input.equals("stop")) {
        break;
    }
}
#

or

BufferedReader reader = new BufferedReader(System.in);
boolean running = true;
while(running) {
    String input = reader.readLine();
    if(input.equals("stop")) {
        running = false;
        continue;
    }
}

if you prefer

atomic iris
compact spire
#

Yeah, I was confused

#

they DM'd me though
(because it's a small world)