James T
James T

Reputation: 3320

Command prompt inside a java program

So I want to make a java application which, while running, can run custom commands (not windows commands, commands for my application) from the command line, I can already do it, but I'd like to have a prompt such as > where you'd type commands, how can I output > and then remove it when it's time to print something else to that line?

Upvotes: 0

Views: 448

Answers (3)

James T
James T

Reputation: 3320

I found a good library, JLine, does exactly what I want.

Upvotes: 0

jpredham
jpredham

Reputation: 2199

I agree with PaoloVictor's recommendation of using Console, but if you're more curious about architecture I might suggest a REPL (Read, Evaluate, Print Loop). Where you would do something like this.

init();
while(1){

   System.out.print("myProgram>");
   String cmd = Console.readLine(String fmt, Object... args);
   evaluate(cmd);
}

Upvotes: 1

PaoloVictor
PaoloVictor

Reputation: 1306

On Java 6, you can use the Console class for that. Specifically, the readLine method. From the API:

public String readLine(String fmt, Object... args)

   Provides a formatted prompt, then reads a single line of text from the console. 

Upvotes: 6

Related Questions