How to change command prompt (console) window title from command line Java app? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!c console window titleCan you set WindowTitle in Launch4j where headerType==console?How to change command prompt directory to our required directory using java codeHow do I parse command line arguments in Bash?How do I get current datetime on the Windows command line, in a suitable format for using in a filename?How do I call one constructor from another in Java?Is there an equivalent of 'which' on the Windows command line?How to pass command line arguments to a rake taskHow can I update the current line in a C# Windows Console App?List all environment variables from command line?How do I run two commands in one line in Windows CMD?Can't start Eclipse - Java was started but returned exit code=13How to import an SQL file using the command line in MySQL?
Could a cockatrice have parasitic embryos?
Is there an efficient way for synchronising audio events real-time with LEDs using an MCU?
How to begin with a paragraph in latex
Is there a way to fake a method response using Mock or Stubs?
What is ls Largest Number Formed by only moving two sticks in 508?
Raising a bilingual kid. When should we introduce the majority language?
Is there a verb for listening stealthily?
Are there existing rules/lore for MTG planeswalkers?
Why aren't road bicycle wheels tiny?
Simulate round-robin tournament draw
Why is arima in R one time step off?
France's Public Holidays' Puzzle
Did war bonds have better investment alternatives during WWII?
What is a 'Key' in computer science?
How would it unbalance gameplay to rule that Weapon Master allows for picking a fighting style?
Is there a possibility to generate a list dynamically in Latex?
false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'
How was Lagrange appointed professor of mathematics so early?
Israeli soda type drink
Can gravitational waves pass through a black hole?
What does こした mean?
TV series episode where humans nuke aliens before decrypting their message that states they come in peace
Is it OK if I do not take the receipt in Germany?
All ASCII characters with a given bit count
How to change command prompt (console) window title from command line Java app?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!c console window titleCan you set WindowTitle in Launch4j where headerType==console?How to change command prompt directory to our required directory using java codeHow do I parse command line arguments in Bash?How do I get current datetime on the Windows command line, in a suitable format for using in a filename?How do I call one constructor from another in Java?Is there an equivalent of 'which' on the Windows command line?How to pass command line arguments to a rake taskHow can I update the current line in a C# Windows Console App?List all environment variables from command line?How do I run two commands in one line in Windows CMD?Can't start Eclipse - Java was started but returned exit code=13How to import an SQL file using the command line in MySQL?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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:WINDOWSsystem32cmd.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%].
java windows command-line
add a comment |
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:WINDOWSsystem32cmd.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%].
java windows command-line
add a comment |
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:WINDOWSsystem32cmd.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%].
java windows command-line
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:WINDOWSsystem32cmd.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%].
java windows command-line
java windows command-line
asked Jun 15 '09 at 2:02
evaldazevaldaz
318137
318137
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Although I haven't tried it myself, in Windows, one can use the Win32 API call to SetConsoleTitle in order to change the title of the console.
However, since this is a call to a native library, it will require the use of something like Java Native Interface (JNI) in order to make the call, and this will only work on Windows 2000 and later.
Edit - A solution using JNI
The following is an example of using JNI in order to change the title of the console window from Java in Windows. To implement this, the prerequiste is some knowledge in C and using the compiler/linker.
First, here's result:

(source: coobird.net)
Disclaimer: This is my first Java application using JNI, so it's probably not going to be a good example of how to use it -- I don't perform any error-checking at all, and I may be missing some details.
The Java program was the following:
class ChangeTitle
private static native void setTitle(String s);
static
System.loadLibrary("ChangeTitle");
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);
setTitle(title);
Thread.sleep(1000);
Basically, the title is changed every 5 seconds by calling the setTitle native method in an external native library called ChangeTitle.
Once the above code is compiled to make a ChangeTitle.class file, the javah command is used to create a C header that is used when creating the C library.
Writing the native library
Writing the library will involve writing the C source code against the C header file generated by javah.
The ChangeTitle.h header was 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"
#endif
/*
* Class: ChangeTitle
* Method: setTitle
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_ChangeTitle_setTitle
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus
#endif
#endif
Now, the implementation, ChangeTitle.c:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <jni.h>
#include "ChangeTitle.h"
JNIEXPORT void JNICALL
Java_ChangeTitle_setTitle(JNIEnv* env, jclass c, jstring s)
const jbyte *str;
str = (*env)->GetStringUTFChars(env, s, NULL);
SetConsoleTitle(str);
(*env)->ReleaseStringUTFChars(env, s, str);
;
A String that is passed into the native function is changed into an UTF-8 encoded C string, which is sent to the SetConsoleTitle function, which, as the function name suggests, changes the title of the console.
(Note: There may be some issues with just passing in the string into the SetConsoleTitle function, but according to the documentation, it does accept Unicode as well. I'm not too sure how well the code above will work when sending in various strings.)
The above is basically a combination of sample code obtained 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 more involved sample code with error-checking, please see the Section 3.2: Accessing Strings and SetConsoleTitle Function pages.
Building the DLL
The part that turned out to take the most amount of time for me to figure out was getting the C files to compile into an DLL that actually could be read without causing an UnsatisfiedLinkError.
After a lot of searching and trying things out, I was able to get the C source to compile to a DLL that could be called from Java. Since I am using MinGW, I found a page form mingw.org which described exactly how to build a DLL for JNI.
Sources:
The Java Native Interface Programmer's Guide and Specification
Chapter 2: Getting Started - Details the process using JNI.
JNI-MinGW-DLL - Building a JNI DLL on MinGW with gcc.
1
Please don't expend all that effort on doing JNI, head on over to jna.dev.java.net. It's a lot easier using JNA than JNI. It's the best thing since Python ctypes.
– paxdiablo
Jun 15 '09 at 7:56
add a comment |
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.
yes...teh command title "My Cool Title" works
– Vincent Ramdhanie
Jun 15 '09 at 2:20
He wants the title to change as the program runs, not just when starting the Java application from the command line interface.
– coobird
Jun 15 '09 at 2:25
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
Jun 15 '09 at 2:34
1
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
Jun 15 '09 at 2:43
Exactly. Runtime.exec spawns new process. And yes, I was looking for solution to update title on the fly.
– evaldaz
Jun 15 '09 at 3:05
add a comment |
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);
add a comment |
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.
Thanks, this was exactly what I was looking for. The (stackoverflow) system works!
– MDCore
Jul 16 '10 at 8:35
3
Downvoting because this doesn't even come close to answering "how do I change the window title IN JAVA?"
– Kevin Wright
Oct 3 '14 at 8:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f994273%2fhow-to-change-command-prompt-console-window-title-from-command-line-java-app%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Although I haven't tried it myself, in Windows, one can use the Win32 API call to SetConsoleTitle in order to change the title of the console.
However, since this is a call to a native library, it will require the use of something like Java Native Interface (JNI) in order to make the call, and this will only work on Windows 2000 and later.
Edit - A solution using JNI
The following is an example of using JNI in order to change the title of the console window from Java in Windows. To implement this, the prerequiste is some knowledge in C and using the compiler/linker.
First, here's result:

(source: coobird.net)
Disclaimer: This is my first Java application using JNI, so it's probably not going to be a good example of how to use it -- I don't perform any error-checking at all, and I may be missing some details.
The Java program was the following:
class ChangeTitle
private static native void setTitle(String s);
static
System.loadLibrary("ChangeTitle");
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);
setTitle(title);
Thread.sleep(1000);
Basically, the title is changed every 5 seconds by calling the setTitle native method in an external native library called ChangeTitle.
Once the above code is compiled to make a ChangeTitle.class file, the javah command is used to create a C header that is used when creating the C library.
Writing the native library
Writing the library will involve writing the C source code against the C header file generated by javah.
The ChangeTitle.h header was 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"
#endif
/*
* Class: ChangeTitle
* Method: setTitle
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_ChangeTitle_setTitle
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus
#endif
#endif
Now, the implementation, ChangeTitle.c:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <jni.h>
#include "ChangeTitle.h"
JNIEXPORT void JNICALL
Java_ChangeTitle_setTitle(JNIEnv* env, jclass c, jstring s)
const jbyte *str;
str = (*env)->GetStringUTFChars(env, s, NULL);
SetConsoleTitle(str);
(*env)->ReleaseStringUTFChars(env, s, str);
;
A String that is passed into the native function is changed into an UTF-8 encoded C string, which is sent to the SetConsoleTitle function, which, as the function name suggests, changes the title of the console.
(Note: There may be some issues with just passing in the string into the SetConsoleTitle function, but according to the documentation, it does accept Unicode as well. I'm not too sure how well the code above will work when sending in various strings.)
The above is basically a combination of sample code obtained 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 more involved sample code with error-checking, please see the Section 3.2: Accessing Strings and SetConsoleTitle Function pages.
Building the DLL
The part that turned out to take the most amount of time for me to figure out was getting the C files to compile into an DLL that actually could be read without causing an UnsatisfiedLinkError.
After a lot of searching and trying things out, I was able to get the C source to compile to a DLL that could be called from Java. Since I am using MinGW, I found a page form mingw.org which described exactly how to build a DLL for JNI.
Sources:
The Java Native Interface Programmer's Guide and Specification
Chapter 2: Getting Started - Details the process using JNI.
JNI-MinGW-DLL - Building a JNI DLL on MinGW with gcc.
1
Please don't expend all that effort on doing JNI, head on over to jna.dev.java.net. It's a lot easier using JNA than JNI. It's the best thing since Python ctypes.
– paxdiablo
Jun 15 '09 at 7:56
add a comment |
Although I haven't tried it myself, in Windows, one can use the Win32 API call to SetConsoleTitle in order to change the title of the console.
However, since this is a call to a native library, it will require the use of something like Java Native Interface (JNI) in order to make the call, and this will only work on Windows 2000 and later.
Edit - A solution using JNI
The following is an example of using JNI in order to change the title of the console window from Java in Windows. To implement this, the prerequiste is some knowledge in C and using the compiler/linker.
First, here's result:

(source: coobird.net)
Disclaimer: This is my first Java application using JNI, so it's probably not going to be a good example of how to use it -- I don't perform any error-checking at all, and I may be missing some details.
The Java program was the following:
class ChangeTitle
private static native void setTitle(String s);
static
System.loadLibrary("ChangeTitle");
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);
setTitle(title);
Thread.sleep(1000);
Basically, the title is changed every 5 seconds by calling the setTitle native method in an external native library called ChangeTitle.
Once the above code is compiled to make a ChangeTitle.class file, the javah command is used to create a C header that is used when creating the C library.
Writing the native library
Writing the library will involve writing the C source code against the C header file generated by javah.
The ChangeTitle.h header was 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"
#endif
/*
* Class: ChangeTitle
* Method: setTitle
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_ChangeTitle_setTitle
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus
#endif
#endif
Now, the implementation, ChangeTitle.c:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <jni.h>
#include "ChangeTitle.h"
JNIEXPORT void JNICALL
Java_ChangeTitle_setTitle(JNIEnv* env, jclass c, jstring s)
const jbyte *str;
str = (*env)->GetStringUTFChars(env, s, NULL);
SetConsoleTitle(str);
(*env)->ReleaseStringUTFChars(env, s, str);
;
A String that is passed into the native function is changed into an UTF-8 encoded C string, which is sent to the SetConsoleTitle function, which, as the function name suggests, changes the title of the console.
(Note: There may be some issues with just passing in the string into the SetConsoleTitle function, but according to the documentation, it does accept Unicode as well. I'm not too sure how well the code above will work when sending in various strings.)
The above is basically a combination of sample code obtained 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 more involved sample code with error-checking, please see the Section 3.2: Accessing Strings and SetConsoleTitle Function pages.
Building the DLL
The part that turned out to take the most amount of time for me to figure out was getting the C files to compile into an DLL that actually could be read without causing an UnsatisfiedLinkError.
After a lot of searching and trying things out, I was able to get the C source to compile to a DLL that could be called from Java. Since I am using MinGW, I found a page form mingw.org which described exactly how to build a DLL for JNI.
Sources:
The Java Native Interface Programmer's Guide and Specification
Chapter 2: Getting Started - Details the process using JNI.
JNI-MinGW-DLL - Building a JNI DLL on MinGW with gcc.
1
Please don't expend all that effort on doing JNI, head on over to jna.dev.java.net. It's a lot easier using JNA than JNI. It's the best thing since Python ctypes.
– paxdiablo
Jun 15 '09 at 7:56
add a comment |
Although I haven't tried it myself, in Windows, one can use the Win32 API call to SetConsoleTitle in order to change the title of the console.
However, since this is a call to a native library, it will require the use of something like Java Native Interface (JNI) in order to make the call, and this will only work on Windows 2000 and later.
Edit - A solution using JNI
The following is an example of using JNI in order to change the title of the console window from Java in Windows. To implement this, the prerequiste is some knowledge in C and using the compiler/linker.
First, here's result:

(source: coobird.net)
Disclaimer: This is my first Java application using JNI, so it's probably not going to be a good example of how to use it -- I don't perform any error-checking at all, and I may be missing some details.
The Java program was the following:
class ChangeTitle
private static native void setTitle(String s);
static
System.loadLibrary("ChangeTitle");
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);
setTitle(title);
Thread.sleep(1000);
Basically, the title is changed every 5 seconds by calling the setTitle native method in an external native library called ChangeTitle.
Once the above code is compiled to make a ChangeTitle.class file, the javah command is used to create a C header that is used when creating the C library.
Writing the native library
Writing the library will involve writing the C source code against the C header file generated by javah.
The ChangeTitle.h header was 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"
#endif
/*
* Class: ChangeTitle
* Method: setTitle
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_ChangeTitle_setTitle
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus
#endif
#endif
Now, the implementation, ChangeTitle.c:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <jni.h>
#include "ChangeTitle.h"
JNIEXPORT void JNICALL
Java_ChangeTitle_setTitle(JNIEnv* env, jclass c, jstring s)
const jbyte *str;
str = (*env)->GetStringUTFChars(env, s, NULL);
SetConsoleTitle(str);
(*env)->ReleaseStringUTFChars(env, s, str);
;
A String that is passed into the native function is changed into an UTF-8 encoded C string, which is sent to the SetConsoleTitle function, which, as the function name suggests, changes the title of the console.
(Note: There may be some issues with just passing in the string into the SetConsoleTitle function, but according to the documentation, it does accept Unicode as well. I'm not too sure how well the code above will work when sending in various strings.)
The above is basically a combination of sample code obtained 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 more involved sample code with error-checking, please see the Section 3.2: Accessing Strings and SetConsoleTitle Function pages.
Building the DLL
The part that turned out to take the most amount of time for me to figure out was getting the C files to compile into an DLL that actually could be read without causing an UnsatisfiedLinkError.
After a lot of searching and trying things out, I was able to get the C source to compile to a DLL that could be called from Java. Since I am using MinGW, I found a page form mingw.org which described exactly how to build a DLL for JNI.
Sources:
The Java Native Interface Programmer's Guide and Specification
Chapter 2: Getting Started - Details the process using JNI.
JNI-MinGW-DLL - Building a JNI DLL on MinGW with gcc.
Although I haven't tried it myself, in Windows, one can use the Win32 API call to SetConsoleTitle in order to change the title of the console.
However, since this is a call to a native library, it will require the use of something like Java Native Interface (JNI) in order to make the call, and this will only work on Windows 2000 and later.
Edit - A solution using JNI
The following is an example of using JNI in order to change the title of the console window from Java in Windows. To implement this, the prerequiste is some knowledge in C and using the compiler/linker.
First, here's result:

(source: coobird.net)
Disclaimer: This is my first Java application using JNI, so it's probably not going to be a good example of how to use it -- I don't perform any error-checking at all, and I may be missing some details.
The Java program was the following:
class ChangeTitle
private static native void setTitle(String s);
static
System.loadLibrary("ChangeTitle");
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);
setTitle(title);
Thread.sleep(1000);
Basically, the title is changed every 5 seconds by calling the setTitle native method in an external native library called ChangeTitle.
Once the above code is compiled to make a ChangeTitle.class file, the javah command is used to create a C header that is used when creating the C library.
Writing the native library
Writing the library will involve writing the C source code against the C header file generated by javah.
The ChangeTitle.h header was 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"
#endif
/*
* Class: ChangeTitle
* Method: setTitle
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_ChangeTitle_setTitle
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus
#endif
#endif
Now, the implementation, ChangeTitle.c:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <jni.h>
#include "ChangeTitle.h"
JNIEXPORT void JNICALL
Java_ChangeTitle_setTitle(JNIEnv* env, jclass c, jstring s)
const jbyte *str;
str = (*env)->GetStringUTFChars(env, s, NULL);
SetConsoleTitle(str);
(*env)->ReleaseStringUTFChars(env, s, str);
;
A String that is passed into the native function is changed into an UTF-8 encoded C string, which is sent to the SetConsoleTitle function, which, as the function name suggests, changes the title of the console.
(Note: There may be some issues with just passing in the string into the SetConsoleTitle function, but according to the documentation, it does accept Unicode as well. I'm not too sure how well the code above will work when sending in various strings.)
The above is basically a combination of sample code obtained 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 more involved sample code with error-checking, please see the Section 3.2: Accessing Strings and SetConsoleTitle Function pages.
Building the DLL
The part that turned out to take the most amount of time for me to figure out was getting the C files to compile into an DLL that actually could be read without causing an UnsatisfiedLinkError.
After a lot of searching and trying things out, I was able to get the C source to compile to a DLL that could be called from Java. Since I am using MinGW, I found a page form mingw.org which described exactly how to build a DLL for JNI.
Sources:
The Java Native Interface Programmer's Guide and Specification
Chapter 2: Getting Started - Details the process using JNI.
JNI-MinGW-DLL - Building a JNI DLL on MinGW with gcc.
edited Mar 22 at 14:59
Glorfindel
16.7k115273
16.7k115273
answered Jun 15 '09 at 2:16
coobirdcoobird
137k31198221
137k31198221
1
Please don't expend all that effort on doing JNI, head on over to jna.dev.java.net. It's a lot easier using JNA than JNI. It's the best thing since Python ctypes.
– paxdiablo
Jun 15 '09 at 7:56
add a comment |
1
Please don't expend all that effort on doing JNI, head on over to jna.dev.java.net. It's a lot easier using JNA than JNI. It's the best thing since Python ctypes.
– paxdiablo
Jun 15 '09 at 7:56
1
1
Please don't expend all that effort on doing JNI, head on over to jna.dev.java.net. It's a lot easier using JNA than JNI. It's the best thing since Python ctypes.
– paxdiablo
Jun 15 '09 at 7:56
Please don't expend all that effort on doing JNI, head on over to jna.dev.java.net. It's a lot easier using JNA than JNI. It's the best thing since Python ctypes.
– paxdiablo
Jun 15 '09 at 7:56
add a comment |
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.
yes...teh command title "My Cool Title" works
– Vincent Ramdhanie
Jun 15 '09 at 2:20
He wants the title to change as the program runs, not just when starting the Java application from the command line interface.
– coobird
Jun 15 '09 at 2:25
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
Jun 15 '09 at 2:34
1
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
Jun 15 '09 at 2:43
Exactly. Runtime.exec spawns new process. And yes, I was looking for solution to update title on the fly.
– evaldaz
Jun 15 '09 at 3:05
add a comment |
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.
yes...teh command title "My Cool Title" works
– Vincent Ramdhanie
Jun 15 '09 at 2:20
He wants the title to change as the program runs, not just when starting the Java application from the command line interface.
– coobird
Jun 15 '09 at 2:25
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
Jun 15 '09 at 2:34
1
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
Jun 15 '09 at 2:43
Exactly. Runtime.exec spawns new process. And yes, I was looking for solution to update title on the fly.
– evaldaz
Jun 15 '09 at 3:05
add a comment |
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.
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.
edited Jun 29 '09 at 5:25
answered Jun 15 '09 at 2:04
dlamblindlamblin
27.6k1876111
27.6k1876111
yes...teh command title "My Cool Title" works
– Vincent Ramdhanie
Jun 15 '09 at 2:20
He wants the title to change as the program runs, not just when starting the Java application from the command line interface.
– coobird
Jun 15 '09 at 2:25
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
Jun 15 '09 at 2:34
1
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
Jun 15 '09 at 2:43
Exactly. Runtime.exec spawns new process. And yes, I was looking for solution to update title on the fly.
– evaldaz
Jun 15 '09 at 3:05
add a comment |
yes...teh command title "My Cool Title" works
– Vincent Ramdhanie
Jun 15 '09 at 2:20
He wants the title to change as the program runs, not just when starting the Java application from the command line interface.
– coobird
Jun 15 '09 at 2:25
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
Jun 15 '09 at 2:34
1
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
Jun 15 '09 at 2:43
Exactly. Runtime.exec spawns new process. And yes, I was looking for solution to update title on the fly.
– evaldaz
Jun 15 '09 at 3:05
yes...teh command title "My Cool Title" works
– Vincent Ramdhanie
Jun 15 '09 at 2:20
yes...teh command title "My Cool Title" works
– Vincent Ramdhanie
Jun 15 '09 at 2:20
He wants the title to change as the program runs, not just when starting the Java application from the command line interface.
– coobird
Jun 15 '09 at 2:25
He wants the title to change as the program runs, not just when starting the Java application from the command line interface.
– coobird
Jun 15 '09 at 2:25
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
Jun 15 '09 at 2:34
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
Jun 15 '09 at 2:34
1
1
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
Jun 15 '09 at 2:43
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
Jun 15 '09 at 2:43
Exactly. Runtime.exec spawns new process. And yes, I was looking for solution to update title on the fly.
– evaldaz
Jun 15 '09 at 3:05
Exactly. Runtime.exec spawns new process. And yes, I was looking for solution to update title on the fly.
– evaldaz
Jun 15 '09 at 3:05
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
edited Jun 15 '12 at 12:34
CSchulz
6,04084592
6,04084592
answered May 14 '10 at 17:37
user341477user341477
311
311
add a comment |
add a comment |
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.
Thanks, this was exactly what I was looking for. The (stackoverflow) system works!
– MDCore
Jul 16 '10 at 8:35
3
Downvoting because this doesn't even come close to answering "how do I change the window title IN JAVA?"
– Kevin Wright
Oct 3 '14 at 8:57
add a comment |
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.
Thanks, this was exactly what I was looking for. The (stackoverflow) system works!
– MDCore
Jul 16 '10 at 8:35
3
Downvoting because this doesn't even come close to answering "how do I change the window title IN JAVA?"
– Kevin Wright
Oct 3 '14 at 8:57
add a comment |
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.
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.
answered Jun 15 '09 at 6:36
Berry TsakalaBerry Tsakala
4,45684260
4,45684260
Thanks, this was exactly what I was looking for. The (stackoverflow) system works!
– MDCore
Jul 16 '10 at 8:35
3
Downvoting because this doesn't even come close to answering "how do I change the window title IN JAVA?"
– Kevin Wright
Oct 3 '14 at 8:57
add a comment |
Thanks, this was exactly what I was looking for. The (stackoverflow) system works!
– MDCore
Jul 16 '10 at 8:35
3
Downvoting because this doesn't even come close to answering "how do I change the window title IN JAVA?"
– Kevin Wright
Oct 3 '14 at 8:57
Thanks, this was exactly what I was looking for. The (stackoverflow) system works!
– MDCore
Jul 16 '10 at 8:35
Thanks, this was exactly what I was looking for. The (stackoverflow) system works!
– MDCore
Jul 16 '10 at 8:35
3
3
Downvoting because this doesn't even come close to answering "how do I change the window title IN JAVA?"
– Kevin Wright
Oct 3 '14 at 8:57
Downvoting because this doesn't even come close to answering "how do I change the window title IN JAVA?"
– Kevin Wright
Oct 3 '14 at 8:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f994273%2fhow-to-change-command-prompt-console-window-title-from-command-line-java-app%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown