Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Although I haven't tried it myself, in Windows, one can use the Win32 API call to <a href="http://msdn.microsoft.com/en-us/library/ms686050(VS.85).aspx" rel="noreferrer"><code>SetConsoleTitle</code></a> in order to change the title of the console.</p> <p>However, since this is a call to a native library, it will require the use of something like <a href="http://en.wikipedia.org/wiki/Java_Native_Interface" rel="noreferrer">Java Native Interface (JNI)</a> in order to make the call, and this will only work on Windows 2000 and later.</p> <p><strong>Edit - A solution using JNI</strong></p> <p>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.</p> <p>First, here's result:</p> <p><a href="http://coobird.net/img/jni-change-console-title.png" rel="noreferrer">Changing the console title from a Java application http://coobird.net/img/jni-change-console-title.png</a></p> <p>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.</p> <p>The Java program was the following:</p> <pre><code>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 &lt; 5; i++) { String title = "Hello! " + i; System.out.println("Setting title to: " + title); setTitle(title); Thread.sleep(1000); } } } </code></pre> <p>Basically, the title is changed every 5 seconds by calling the <code>setTitle</code> native method in an external native library called <code>ChangeTitle</code>.</p> <p>Once the above code is compiled to make a <code>ChangeTitle.class</code> file, the <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/javah.html" rel="noreferrer"><code>javah</code></a> command is used to create a C header that is used when creating the C library.</p> <p><strong>Writing the native library</strong></p> <p>Writing the library will involve writing the C source code against the C header file generated by <code>javah</code>.</p> <p>The <code>ChangeTitle.h</code> header was the following:</p> <pre><code>/* DO NOT EDIT THIS FILE - it is machine generated */ #include &lt;jni.h&gt; /* 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 </code></pre> <p>Now, the implementation, <code>ChangeTitle.c</code>:</p> <pre><code>#include &lt;windows.h&gt; #include &lt;stdio.h&gt; #include &lt;conio.h&gt; #include &lt;jni.h&gt; #include "ChangeTitle.h" JNIEXPORT void JNICALL Java_ChangeTitle_setTitle(JNIEnv* env, jclass c, jstring s) { const jbyte *str; str = (*env)-&gt;GetStringUTFChars(env, s, NULL); SetConsoleTitle(str); (*env)-&gt;ReleaseStringUTFChars(env, s, str); }; </code></pre> <p>A <code>String</code> that is passed into the native function is changed into an UTF-8 encoded C string, which is sent to the <a href="http://msdn.microsoft.com/en-us/library/ms686050(VS.85).aspx" rel="noreferrer"><code>SetConsoleTitle</code> function</a>, which, as the function name suggests, changes the title of the console.</p> <p>(Note: There may be some issues with just passing in the string into the <code>SetConsoleTitle</code> 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.)</p> <p>The above is basically a combination of sample code obtained from <a href="http://java.sun.com/docs/books/jni/html/objtypes.html#4001" rel="noreferrer">Section 3.2: Accessing Strings</a> of <a href="http://java.sun.com/docs/books/jni/html/jniTOC.html" rel="noreferrer">The Java Native Interface Programmer's Guide and Specification</a>, and the <a href="http://msdn.microsoft.com/en-us/library/ms686050(VS.85).aspx" rel="noreferrer"><code>SetConsoleTitle</code> Function</a> page from MSDN.</p> <p>For a more involved sample code with error-checking, please see the <a href="http://java.sun.com/docs/books/jni/html/objtypes.html#4001" rel="noreferrer">Section 3.2: Accessing Strings</a> and <a href="http://msdn.microsoft.com/en-us/library/ms686050(VS.85).aspx" rel="noreferrer"><code>SetConsoleTitle</code> Function</a> pages.</p> <p><strong>Building the DLL</strong></p> <p>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 <a href="http://java.sun.com/javase/6/docs/api/java/lang/UnsatisfiedLinkError.html" rel="noreferrer"><code>UnsatisfiedLinkError</code></a>.</p> <p>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 <code>mingw.org</code> which <a href="http://www.mingw.org/node/41" rel="noreferrer">described exactly how to build a DLL for JNI</a>.</p> <p>Sources:</p> <ul> <li><a href="http://java.sun.com/docs/books/jni/html/jniTOC.html" rel="noreferrer">The Java Native Interface Programmer's Guide and Specification</a> <ul> <li><a href="http://java.sun.com/docs/books/jni/html/start.html#769" rel="noreferrer">Chapter 2: Getting Started</a> - Details the process using JNI.</li> </ul></li> <li><a href="http://www.mingw.org/node/41" rel="noreferrer">JNI-MinGW-DLL</a> - Building a JNI DLL on MinGW with gcc.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload