views:

1910

answers:

4

How to change and update the title of the command prompt window from the java command line application? Every time I run my application, the command prompt window title shows: C:\WINDOWS\system32\cmd.exe - java MyApp.

I'd like to change and update the window title as the java program runs, for example as wget(win32) updates downloading status in the title: Wget [12%].

+2  A: 

This depends on your terminal emulator, but essentially it's just printing out control sequences to the console.

Now I'm not clear on what control sequences CMD.EXE responds to (I haven't one available to try this on) but I hear there's a command called TITLE which sets the title of the window. I tried piping TITLE's output to a file, but apparently, it doesn't actually set the title by outputting control characters. The START command can take a parameter which is title of the window followed by the command to run in the window. So something like

cmd TITLE "lovely Application that is in a command window." && "java" MyApp
REM or
start "lovely Application that is java based." java MyApp

Personally I would just bundle the whole thing with a shortcut where you can edit the properties such as the current directory, the command, it's parameters, and the window size, style and title (if I remember rightly). Give it a nice icon and people will use it.

dlamblin
yes...teh command title "My Cool Title" works
Vincent Ramdhanie
He wants the title to change as the program runs, not just when starting the Java application from the command line interface.
coobird
Yeah I know, so I was telling someone to find the escape sequence for the title change in cmd (which must exist but I can't find it, even in ANSI.SYS), or the asker could use Java.lang.runtime to exec the title command when it's needed. If that works on the same window.
dlamblin
Unfortunately using Runtime.exec with "title" doesn't work -- at least from what I tried, I wasn't able to get it to work. I suspect it's because Runtime.exec will start a new process separate from the current process running java.exe.
coobird
Exactly. Runtime.exec spawns new process. And yes, I was looking for solution to update title on the fly.
evaldaz
+8  A: 
coobird
Please don't expend all that effort on doing JNI, head on over to https://jna.dev.java.net/. It's a lot easier using JNA than JNI. It's the best thing since Python ctypes.
paxdiablo
+2  A: 

following dlamblin's revelation ;-) here's a python code. note that there are 2 different commands in most programming languages:

  • system
  • exec

system will issue a system command, exec indeed spawns a new process. thus:

C:\>python
>>> import os
>>> os.system("title berry tsakala")

which works inside a running program. Just find the java equivalent.

Berry Tsakala
Thanks, this was exactly what I was looking for. The (stackoverflow) system works!
MDCore
A: 

Here's my solution using JNA:

import com.sun.jna.Library;

import com.sun.jna.Native;

import com.sun.jna.Platform;

public class SetTitle {

public interface CLibrary extends Library {
    CLibrary INSTANCE = (CLibrary)
        Native.loadLibrary((Platform.isWindows() ? "kernel32" : "c"),
                           CLibrary.class);

    boolean SetConsoleTitleA(String title);
}

public static void main(String[] args) {
    CLibrary.INSTANCE.SetConsoleTitleA("Testing 123");
    System.exit(0);
}

}