[ACCEPTED]-How to change command prompt (console) window title from command line Java app?-command-line

Accepted answer
Score: 22

Although I haven't tried it myself, in Windows, one 48 can use the Win32 API call to SetConsoleTitle in order 47 to change the title of the console.

However, since 46 this is a call to a native library, it will 45 require the use of something like Java Native Interface (JNI) in order 44 to make the call, and this will only work 43 on Windows 2000 and later.

Edit - A solution using JNI

The following 42 is an example of using JNI in order to change 41 the title of the console window from Java 40 in Windows. To implement this, the prerequiste 39 is some knowledge in C and using the compiler/linker.

First, here's 38 result:

Changing the console title from a Java application
(source: coobird.net)

Disclaimer: This is my first Java 37 application using JNI, so it's probably 36 not going to be a good example of how to 35 use it -- I don't perform any error-checking 34 at all, and I may be missing some details.

The 33 Java program was the following:

class ChangeTitle {

    private static native void setTitle(String s);

    static {

    public static void main(String[] args) throws Exception {

        for (int i = 0; i < 5; i++) {
            String title = "Hello! " + i;
            System.out.println("Setting title to: " + title);

Basically, the 32 title is changed every 5 seconds by calling 31 the setTitle native method in an external native 30 library called ChangeTitle.

Once the above code is compiled 29 to make a ChangeTitle.class file, the javah command is used to 28 create a C header that is used when creating 27 the C library.

Writing the native library

Writing the library will involve 26 writing the C source code against the C 25 header file generated by javah.

The ChangeTitle.h header was 24 the following:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class ChangeTitle */

#ifndef _Included_ChangeTitle
#define _Included_ChangeTitle
#ifdef __cplusplus
extern "C" {
 * Class:     ChangeTitle
 * Method:    setTitle
 * Signature: (Ljava/lang/String;)V
JNIEXPORT void JNICALL Java_ChangeTitle_setTitle
  (JNIEnv *, jclass, jstring);

#ifdef __cplusplus

Now, the implementation, ChangeTitle.c:

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <jni.h>
#include "ChangeTitle.h"

Java_ChangeTitle_setTitle(JNIEnv* env, jclass c, jstring s) {
    const jbyte *str;
    str = (*env)->GetStringUTFChars(env, s, NULL);


    (*env)->ReleaseStringUTFChars(env, s, str);

A 23 String that is passed into the native function 22 is changed into an UTF-8 encoded C string, which 21 is sent to the SetConsoleTitle function, which, as the function 20 name suggests, changes the title of the 19 console.

(Note: There may be some issues 18 with just passing in the string into the 17 SetConsoleTitle function, but according to the documentation, it 16 does accept Unicode as well. I'm not too 15 sure how well the code above will work when 14 sending in various strings.)

The above is 13 basically a combination of sample code obtained 12 from Section 3.2: Accessing Strings of The Java Native Interface Programmer's Guide and Specification, and the SetConsoleTitle Function page from MSDN.

For a 11 more involved sample code with error-checking, please 10 see the Section 3.2: Accessing Strings and SetConsoleTitle Function pages.

Building the DLL

The part that turned 9 out to take the most amount of time for 8 me to figure out was getting the C files 7 to compile into an DLL that actually could 6 be read without causing an UnsatisfiedLinkError.

After a lot 5 of searching and trying things out, I was 4 able to get the C source to compile to a 3 DLL that could be called from Java. Since 2 I am using MinGW, I found a page form mingw.org which 1 described exactly how to build a DLL for JNI.


Score: 5

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

Now I'm not clear 16 on what control sequences CMD.EXE responds 15 to (I haven't one available to try this 14 on) but I hear there's a command called 13 TITLE which sets the title of the window. I tried 12 piping TITLE's output to a file, but apparently, it 11 doesn't actually set the title by outputting 10 control characters. The START command can 9 take a parameter which is title of the window 8 followed by the command to run in the window. So 7 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 6 the whole thing with a shortcut where you 5 can edit the properties such as the current 4 directory, the command, it's parameters, and 3 the window size, style and title (if I remember 2 rightly). Give it a nice icon and people 1 will use it.

Score: 3

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"),

        boolean SetConsoleTitleA(String title);

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


Score: 0

You can use the CLITools Java library


More Related questions